#include<cstdio>
#include<iostream>
using namespace std;
void get(string t,int nextval[])
{
int i=0;
int j=-1;
nextval[0]=-1;
while(i<t.length()){
if(j==-1 || t[i]==t[j]){
i++; j++;
if(t[i]!=t[j])
nextval[i]=j;
else
nextval[i]=nextval[j];
}
else j=nextval[j];
}
}
void kmp(string s,string t){
int i=0;
int j=0;
int nextval[100];
get(t,nextval);
for(int q=0;q<5;q++)
cout<<nextval[q]<<endl;
cout<<endl;
while(i<s.length() && j<t.length()){
if(j==-1 || s[i]==t[j]) {i++; j++;}
else j=nextval[j];
}
cout<<j<<endl;
cout<<t.length()<<endl;
cout<<endl;
if(j>t.length()) //为什么这里就<t.length()还是输出yes呢?
cout<<"yes";
else
cout<<"no";
}
int main(){
string s;
string t;
cin>>s;
cin>>t;
kmp(s,t);
}
为什么在void函数中用 if 分支时不能跳转到else
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- 翟码农 2019-03-23 17:09关注
你这问题,网上有好多帖子提到呢。意思就是-1和5比,-1会从有符号类型转为无符号类型,从而就比5大了。我不是写C++的, 你可以试试在比较前,再定义一个int变量k来接收j的值,再用k比较,会如何。
解决 无用评论 打赏 举报