Manacher【SP7586】NUMOFPAL - Number of Palindromes

來源:互聯網
上載者:User

標籤:迴文串   ==   can   ace   iostream   字元   ring   def   半徑   

Description

求一個串中包含幾個迴文串。

Input

輸入一個字串\(S\)

Output

包含的迴文串的個數.

看到題解裡面有人人寫迴文自動機.

有必要那麼麻煩嘛 emmm

我們直接跑\(Manacher\)就好了啊.

答案就是以每一位為中心的迴文串長度/2的和。

(如果添加字元則為迴文半徑長度/2。)

這個就不多解釋了.很明顯的一個東西。

不能理解的話,可以看下這個

# a # a # b # a # a #

1 2 3 2 1 6 1 2 3 2 1

數字對應於每一個位置的迴文半徑

.(實際上減去\(1\)才是,但是計算的時候要加上1,所以代碼裡面直接用了這個.)

代碼

#include<cstdio>#include<algorithm>#include<iostream>#include<cstring>#define R registerusing namespace std;const int maxn=1008;char ch[maxn];char s[maxn<<2];int len,RL[maxn<<2],MaxRight,center,ans;int main(){    scanf("%s",ch);    len=strlen(ch);    for(R int i=0;i<len;i++)s[2*i+1]=ch[i];    len=2*len+1;    for(R int i=0;i<len;i++)    {        if(i<=MaxRight)            RL[i]=min(RL[2*center-i],MaxRight-i);        else RL[i]=1;        while(s[i-RL[i]]==s[i+RL[i]] and i-RL[i]>=0 and i+RL[i]<len)            RL[i]++;        if(i+RL[i]-1>MaxRight)            MaxRight=i+RL[i]-1,center=i;    }    for(R int i=0;i<len;i++)ans+=RL[i]/2;    printf("%d",ans);}

Manacher【SP7586】NUMOFPAL - Number of Palindromes

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.