[USACO15FEB]Censoring S
阿新 • • 發佈:2020-10-18
題目連結:https://www.luogu.com.cn/problem/P4824
思路: 考慮kmp 匹配 然後 因為要刪除掉 那麼考慮用一個棧來維護刪除 如果用陣列記錄那些位置刪的話 不好處理之前刪除的位置
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 #define pb push_back 5 const int maxn =1e6+10; 6 const int mod=998244353; 7 int nxt[maxn]; 8 int f[maxn]; 9View Code10 11 int main() 12 { 13 ios::sync_with_stdio(false); 14 cin.tie(0); 15 string s,t; 16 cin>>s>>t; 17 stack<int>st; 18 int n=s.size(); 19 20 int len=t.size(); 21 int j=0; 22 for(int i=2;i<=len;i++) 23 { 24 while(j&&t[i-1]!=t[j])25 j=nxt[j]; 26 if(t[i-1]==t[j]) 27 j++; 28 nxt[i]=j; 29 } 30 j=0; 31 for(int i=1;i<=n;i++) 32 { 33 st.push(i); 34 while(j&&s[i-1]!=t[j]) 35 j=nxt[j]; 36 if(s[i-1]==t[j]) 37 j++; 38 f[i]=j;39 if(j==len) 40 { 41 for(int x=0;x<len;x++) 42 { 43 st.pop(); 44 } 45 if(!st.empty()) 46 j=f[st.top()]; 47 else 48 j=0; 49 } 50 } 51 vector<int>cnt; 52 while(!st.empty()) 53 { 54 cnt.pb(st.top()); 55 st.pop(); 56 } 57 reverse(cnt.begin(),cnt.end()); 58 for(auto &v:cnt) 59 cout<<s[v-1]; 60 61 62 63 64 }