Advertisement
RaFiN_

aho corasick without pointer

Mar 23rd, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1.  
  2.  
  3. const int maxn = 160 * 80;// sum of length of all patterns...
  4.  
  5. string ss[MAX],str;int sz,to[maxn][30],link[maxn];vi ending[maxn];int End[200],len;
  6.  
  7. void CLEAR(){
  8.     sz = 0;
  9.     mem(to,0);
  10.     mem(link,0);
  11.     mem(End,0);
  12.     for(int i = 0;i<maxn;i++)ending[i].clear();
  13.  
  14. }
  15.  
  16. void Insert(string s,int indx){
  17.  
  18.     int curr = 0;
  19.     for(int i = 0;i<(int)s.length();i++){
  20.  
  21.         int id = s[i] - 'a';
  22.         if(!to[curr][id])to[curr][id] = ++sz;
  23.         curr = to[curr][id];
  24.  
  25.     }
  26.     ending[curr].pb(indx);
  27.  
  28. }
  29.  
  30. void propagate(int pos){
  31.  
  32.     for(auto it : ending[link[pos]]){
  33.  
  34.         ending[pos].pb(it);
  35.     }
  36. }
  37.  
  38. void failure()
  39. {
  40.     int curr = 0;
  41.     link[curr] = -1;
  42.     queue<int>q;
  43.     q.push(curr);
  44.     while(!q.empty()){
  45.  
  46.         int x = q.front();
  47.         q.pop();
  48.         for(int i = 0;i<26;i++){
  49.  
  50.             if(!to[x][i])continue;
  51.             if(!x)link[to[x][i]] = 0;
  52.             else {
  53.                 int y = x;
  54.                 while(link[y]!=-1){
  55.  
  56.                     if(to[link[y]][i]){
  57.  
  58.                         link[to[x][i]] = to[link[y]][i];
  59.                         break;
  60.                     }
  61.                     y = link[y];
  62.                 }
  63.                 if(link[y]==-1)link[to[x][i]] = 0;
  64.             }
  65.             propagate(to[x][i]);
  66.             q.push(to[x][i]);
  67.  
  68.         }
  69.     }
  70. }
  71.  
  72. void Search(){
  73.     int curr = 0;
  74.     int indx = 0;
  75.     while(indx<len){
  76.         int id = str[indx] - 'a';
  77.         while(curr!=-1&&!to[curr][id])curr = link[curr];
  78.         if(curr==-1)curr = 0;
  79.         curr = to[curr][id];
  80.         for(int i = 0;i<ending[curr].size();i++){
  81.             End[ending[curr][i]]++;
  82.         }
  83.         indx++;
  84.     }
  85. }
  86.  
  87.  
  88. int main()
  89. {
  90.     booster()
  91.     ///read("input.txt");
  92.  
  93.     while(1){
  94.         CLEAR();
  95.         int n;
  96.         cin>>n;
  97.         if(!n)break;
  98.         for(int i = 0;i<n;i++){
  99.             cin>>ss[i];
  100.             Insert(ss[i],i);
  101.         }
  102.         failure();
  103.         cin>>str;
  104.         len = str.length();
  105.         Search();
  106.         int mx = 0;
  107.         for(int i = 0;i<n;i++){
  108.             mx = max(mx,End[i]);
  109.         }
  110.         cout<<mx<<endl;
  111.         for(int i = 0;i<n;i++){
  112.             if(mx==End[i])cout<<ss[i]<<endl;
  113.         }
  114.     }
  115.  
  116.  
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement