manacher模板
Posted wans-caesar-02111007
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了manacher模板相关的知识,希望对你有一定的参考价值。
考试竟然写错了manacher!太耻辱了!所以赶快又敲了一遍模板!!一定不能错了aaaa
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 6 char a[11000005], M[22000010]; 7 int max_r[22000010], ans; 8 9 void manacher ( ) { 10 int len = strlen ( a ); 11 M[0] = ‘@‘; M[1] = ‘#‘; 12 for ( int i = 0; i < len; i ++ ) { 13 M[i*2+2] = a[i]; 14 M[i*2+3] = ‘#‘; 15 } 16 M[len*2+2] = ‘$‘; 17 int center = 0, mx = 0; 18 for ( int i = 1; i <= len * 2 + 2; i ++ ) { 19 if ( mx > i ) max_r[i] = min ( mx - i, max_r[center * 2 - i] ); 20 else max_r[i] = 1; 21 while ( M[i + max_r[i]] == M[i - max_r[i]] ) max_r[i] ++; 22 if ( mx < i + max_r[i] ) { 23 mx = i + max_r[i]; center = i; 24 } 25 ans = max ( ans, max_r[i] - 1 ); 26 } 27 } 28 29 int main ( ) { 30 scanf ( "%s", a ); 31 manacher ( ); 32 printf ( "%d", ans ); 33 return 0; 34 }
以上是关于manacher模板的主要内容,如果未能解决你的问题,请参考以下文章