Tranvick

Suffix_automat

Nov 27th, 2011
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <string>
  2. #include <map>
  3. #include <iostream>
  4. using namespace std;
  5. #define N 111111
  6.  
  7. struct _SA{
  8.     int link,len;
  9.     map<char,int> next;
  10. } st[N+N];
  11. int sz,last;
  12.  
  13. void init(){
  14.     last=0;
  15.     sz=1;
  16.     st[0].link=-1;
  17.     st[0].len=0;
  18. }
  19.  
  20. void ins(char c){
  21.     int cur=sz++;
  22.     st[cur].len=st[last].len+1;
  23.     int p;
  24.     for (p=last;p!=-1 && !st[p].next.count(c);p=st[p].link) st[p].next[c]=cur;
  25.     if (p==-1) st[cur].link=0;
  26.     else{
  27.         int q=st[p].next[c];
  28.         if (st[p].len+1==st[q].len) st[cur].link=q;
  29.         else{
  30.             int clone=sz++;
  31.             st[clone]=st[q];
  32.             st[clone].len=st[p].len+1;
  33.             for(;p!=-1 && st[p].next[c]==q;p=st[p].link) st[p].next[c]=q;
  34.             st[q].link=st[cur].link=clone;
  35.         }
  36.     }
  37.     last=cur;
  38. }
  39.  
  40.  
  41. int main(){
  42.     string s;
  43.     cin>>s;
  44.     init();
  45.     for (int i=0;i<s.length();i++) ins(s[i]);
  46.     string t;
  47.     cin>>t;
  48.     int cur=0;
  49.     for (int i=0;i<t.length();i++){
  50.         if (!st[cur].next.count(t[i])){
  51.             cout<<"NO";
  52.             return 0;
  53.         }else cur=st[cur].next[t[i]];
  54.     }
  55.     cout<<"YES";
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment