题目描述
求子串的 nextnextnext 值,用 next
数组存放,全部输出。
输入
输入一个字符串(字符串长度小于等于 100100100)。
输出
输出所有 nextnextnext 值。
输入输出样例
样例输入 #1
复制
abaabcac
样例输出 #1
复制
-1 0 0 1 1 2 0 1
代码:
#include<bits/stdc++.h>
using namespace std;
char a[100];
int i,nextt[100];
void getNext(char p[100],int nextt[100])
{
int j=0,k=-1;
nextt[0]=-1;;
while(j<strlen(p)-1)
{
if(p[j]==p[k]||k==-1)
{
j++;
k++;
nextt[j]=k;
}
else
k=nextt[k];
}
}//KMP中next数组算法
int main()
{
cin>>a;
getNext(a,nextt);
for(int i=0;i<strlen(a);i++)
cout<<nextt[i]<<" ";
return 0;
}