标签:ota fine set stream cout bit tor theme ack
InputThe integer N in the first line denotes the total number of songs in the festival. Each of the following N lines consists of one string, indicating the notes of the i-th (1 <= i <= N) song. The length of the string will not exceed 10^6.OutputThere will be N lines in the output, where the i-th line denotes the maximum possible length of the theme section of the i-th song.
Sample Input
5 xy abc aaa aaaaba aaxoaaaaa
Sample Output
0 0 1 1 2
题意:找三个子串,第一个必须在开头
题解:kmp,刚开始想错了,以为是枚举,tle了,题目又看了一遍才明白意思,用next处理能快很多
不用遍历了
#include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<vector> #include<cstdio> #include<iomanip> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define pi acos(-1) #define ll long long #define mod 1000000007 using namespace std; const int N=1000000+5,maxn=1000000+5,inf=1e9+5; int Next[N]; void getnext(string str) { int k=-1; Next[0]=-1; for(int i=1;i<str.size();i++) { while(k>-1&&str[k+1]!=str[i])k=Next[k]; if(str[k+1]==str[i])k++; Next[i]=k; } } bool kmp(string ptr,string str) { int k=-1,ans=0; for(int i=0;i<ptr.size();i++) { while(k>-1&&str[k+1]!=ptr[i])k=Next[k]; if(str[k+1]==ptr[i])k++; if(k==str.size()-1)return 1; } return 0; } int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin>>t; while(t--){ string ptr; cin>>ptr; getnext(ptr); int k=Next[ptr.size()-1],ans=0; while(k!=-1){ string str=ptr.substr(0,k+1),p=ptr.substr(k+1,ptr.size()-2*k-2); // cout<<p<<" "<<str<<endl; if((k+1)*3<=ptr.size()&&kmp(p,str)) { ans=k+1; break; } k=Next[k]; } cout<<ans<<endl; } return 0; }
标签:ota fine set stream cout bit tor theme ack
原文地址:http://www.cnblogs.com/acjiumeng/p/6843170.html