最長雙迴文串(模板+dp)

來源:互聯網
上載者:User

標籤:scan   man   max   color   blank   pre   def   c++   int   

題目連結

 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 inline ll read(){ 5     int x = 0, f = 1; char ch = getchar(); 6     while(ch > ‘9‘ || ch < ‘0‘){if (ch == ‘-‘) f = -1; ch = getchar();} 7     while(ch >= ‘0‘ && ch <= ‘9‘){ x = x*10+ch-‘0‘; ch = getchar();} 8     return x*f; 9 }10 11 /************************************************************************/12 13 const int maxn = 1e6+7;14 char s[maxn];15 char str[maxn];16 int dp1[maxn], p[maxn];17 int dp2[maxn];18 int len;19 20 void manacher(char s[], int len){21     int ans = 0;22     for(int i = 1;i <= len;i++){23         str[i<<1] = s[i];24         str[(i<<1)+1] = ‘#‘;25     }26     str[1] = ‘#‘; str[len*2+1] = ‘#‘;27     str[0] = ‘&‘; str[len*2+2] = ‘$‘;28     len = len*2+1;29     int j = 0, k;30     for(int i = 1;i <= len;){31         while(str[i-j-1] == str[i+j+1]) j++;32         p[i] = j; 33         if(j > ans) ans = j;34         for(k = 1;k <= j && (p[i]-k != p[i-k]);k++){35             p[i+k] = min(p[i-k], p[i] - k);36         }37         i += k;38         j = max(j-k, 0);39     }40 }41 42 int main(){43     scanf("%s", s+1);44     len = strlen(s+1);45     manacher(s, len);46     len = len*2+1;47     cout << "str: " << str << endl;48     for(int i = 1;i <= len;i++){49         for(int j = p[i];j >= 1;j--){50             if(dp1[i+j] >= j) break;51             dp1[i+j] = j;52         }53         for(int j = p[i];j >= 1;j--){54             if(dp2[i-j] >= j) break;55             dp2[i-j] = j;56         }57     }58     int ans = 0;59     for(int i = 1;i <= len-2;i++){60         if(dp1[i] && dp2[i])61             ans = max(ans, dp1[i] + dp2[i]);62     }63     cout << ans << endl;64     return 0;65 }

 

最長雙迴文串(模板+dp)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.