美文网首页
无标题文章

无标题文章

作者: 未来不是梦而是汗水 | 来源:发表于2017-09-28 20:48 被阅读0次

include<iostream>

include <cstring>

include <algorithm>

include <cmath>

include <queue>

include <stack>

using namespace std;
//next[i]存的是从0到I的字符串的前后相等的最大长度。
void buildNext(string s,int next[]){
next[0]=-1;
for (int i=1; i<=s.length()-1; i++) {
string ss=s.substr(0,i+1);//获取从0开始到I的字符串。
int len=i+1-1;//从最大的长度开始检验。
while (len>=1) {
string s1=ss.substr(i+1-len,len);
string s2=ss.substr(0,len);
if(s1==s2)break;
len--;
}
next[i]=len;
}
}
int KMP(string s,string p,int next[]){
for (int i=0; i<s.length(); ) {
int a=i;
for (int j=0; j<p.length(); ) {
if(s[a]==p[j]){
a++;
j++;
if(j==p.length())return i;
}
else{
if(j>0){
int iNext=j-next[j-1];//移动位数=已匹配的字符数-前后相等的长度;
i+=iNext;
break;
}else{
i++;
break;
}
}
}
}
return -1;
}
int main(){
string s,p;
getline(cin,s);
getline(cin,p);
cout<<s<<endl<<p<<endl;
int next[100];
buildNext(p, next);
cout<<KMP(s, p, next);
return 0;
}

相关文章

  • 无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章 无标题文章无标题文章无标题文章无...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • fasfsdfdf

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章

  • 无标题文章

    无标题文章 无标题文章 无标题文章无标题文章 无标题文章 无标题文章

网友评论

      本文标题:无标题文章

      本文链接:https://www.haomeiwen.com/subject/wtwvextx.html