(本人第一篇c++题解) 这道题很简单,用库函数就行了。 需要注意,find找不到返回的是string::nops。 代码:
#include<bits/stdc++.h>
using namespace std;
int a,b,q,c;
string j,s;
bool d;
int main(){
cin>>q;
cin>>j;
for(int i=1;i<=q;i++) {
cin>>c;
if(c==1) {
cin>>s;
j=j+s;
cout<<j<<endl;
}
if(c==2) {
cin>>a>>b;
j=j.substr(a,b);
cout<<j<<endl;
}
if(c==3) {
cin>>a>>s;
j.insert(a,s);
cout<<j<<endl;
}
if(c==4) {
cin>>s;
if (j.find(s)==string::npos) {//注意
cout<<-1<<endl;
} else {
cout<<j.find(s)<<endl;
}
}
}
return 0;
}本人自创不易,如有雷同,纯属巧合。