作用:求一个字符串中的最长子串,同时还可以求所有子串的长度。
题目链接:
https://vjudge.net/contest/254692#problem/B
最长回文串长度的代码:
1 int Manacher(string s) 2 { 3 string t = "$#"; 4 for (int i = 0; i < s.size(); ++i) 5 { 6 t += s[i]; 7 t += "#"; 8 } 9 vector<int> p(t.size(), 0); 10 int maxx=0; 11 int mx = 0, id = 0, resLen = 0, resCenter = 0; 12 for (int i = 1; i < t.size(); ++i) 13 { 14 // maxx=max(maxx,p[i]); 15 p[i] = mx > i ? min(p[2 * id - i], mx - i) : 1; 16 while (t[i + p[i]] == t[i - p[i]]) 17 ++p[i]; 18 if (mx < i + p[i]) 19 { 20 mx = i + p[i]; 21 id = i; 22 } 23 if (resLen < p[i]) 24 { 25 resLen = p[i]; 26 resCenter = i; 27 } 28 maxx=max(maxx,p[i]); 29 } 30 return maxx-1; 31 }
代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 # define maxn 20000+10 4 char str1[maxn]; 5 char str2[maxn*2]; 6 int p[maxn*2]; 7 int l=0; 8 int ma() 9 { 10 int id=0,mx=0; 11 int ans=0; 12 str2[l]='