如题,代码如下:
#include<bits/stdc++.h>
using namespace std;
string s;
char c[1000010];
int pre[1000010],nxt[1000010];
string t;
struct dat{
int first,len;//first:第一次出现的位置 len:目前匹配到的长度
};
stack<dat> st;
int main(){
// freopen("P4824_2.in","r",stdin);
cin>>s>>t;
int n=s.size();
for(int i=0;i<n;i++){
c[i+1]=s[i];
nxt[i+1]=i+2;
pre[i+1]=i;
}
nxt[0]=1;
pre[n+1]=n;
for(int i=1;i!=n+1;i=nxt[i]){
// cout<<i<<":";
// if(st.size())cout<<"["<<c[i]<<"/"<<t[st.top().len]<<"]";
if(st.size()&&c[i]==t[st.top().len]){
dat cur=st.top();
st.pop();
if(cur.len==t.size()-1){
nxt[pre[cur.first]]=nxt[i];
pre[nxt[i]]=pre[cur.first];
}else st.push({cur.first,cur.len+1});
}else if(c[i]==t[0]){
st.push({i,1});
}
// stack<dat> cur=st;
// while(cur.size()){
// cout<<cur.top().first<<"+"<<cur.top().len<<" ";
// cur.pop();
// }
// cout<<",";
// for(int j=1;j!=n+1;j=nxt[j])cout<<c[j];
// cout<<endl;
}
for(int i=nxt[0];i!=n+1;i=nxt[i])cout<<c[i];
}
有如下一组反例(#2):
hhehhhehhhehhhehhhehhehehhehhehhhhhehhhehhhehhhehhhhehhhehhehhhehhhhhehhhehhhehhhehhhehhehehhehehhhehhhehhehehhhhehhhehhhehhhehhhehhehehhehehhhhehhhehhhehhehehhehhehhehehhhehhhehhhehhhehhhehhehehhehhehehhehhhehhhhhehhhehhhhhehhhehhhehhhehhhehhehehhehehhhehhhhehhhehhhehhheehhehhehehhehhehhehehhehhhehhhehhhehhhhehhhehhhehhhehhhehhehehhehhehhhehhehehhehehhhehhhehhehhhehhhehhhehhhehhhehhehehhehhhehhhhhehhhehhhehhhehhhehhhehhehehhehhhehhhehhhehhhehhehehhehehhhehhhehhehehhehhhehehhehhhhehhhehhhehheheh
hhehhhehhhehhhehhhehhehehheh
我的代码似乎没有删除干净,正确输出如下:
hehhhehhehhhehhhhhehhhehhhehhhehhhhehhhehhhehhheehhehhehehhehhehhehehhehehhhehhhehhehehhehhhhehhhehhhehheheh
求指教。