hdu1358 利用next【】数组求重复前缀

自己用暴力做的,超了,网上搜了之后才知道还带这样滴;

#include<iostream>
#include <string>
//#include <window.h>
//#include <system>
using namespace std;
const int Max=1000011;
int next[Max];
//int flag[Max];

string str;
void get_next(int n)
{
int j=-1;
int i=0;
while(i<n)
{
if(j==-1||str[i]==str[j])
{ j++;i++;
next[i]=j ;
}
else
{
j=next[j];
}
}
}
int main()
{
int NO=1;
int n;

while(cin>>n)
{
if(n==0)
break;
cin>>str;
int i,j;
memset(next,0,sizeof(next));
next[0]=-1;
get_next(n);

cout<<"Test case #"<<NO++<<endl;
for(i=2;i<=n;i++)
{
int temp=i%(i-next[i-1]-1);
int temp2=i/(i-next[i-1]-1);
if(temp==0)
if(i-next[i-1]-1==1&&str[i-1]==str[i-2])
cout<<i<<" "<<i<<endl;
else
if(i-next[i-1]-1!=1&&str[i-1]==str[(i-next[i-1]-1)-1])
cout<<i<<" "<<i/(i-next[i-1]-1)<<endl;
}
cout<<endl;

}
return 0;
}

 

posted @ 2012-03-24 10:38  orangeblog  阅读(186)  评论(0编辑  收藏  举报