程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> UVA 1449 Dominating Patterns (LA4670) 出現次數最多的子串 ac自動機

UVA 1449 Dominating Patterns (LA4670) 出現次數最多的子串 ac自動機

編輯:C++入門知識

Description

 
The archaeologists are going to decipher a very mysterious ``language". Now, they know many language patterns; each pattern can be treated as a string on English letters (only lower case). As a sub string, these patterns may appear more than one times in a large text string (also only lower case English letters).

What matters most is that which patterns are the dominating patterns. Dominating pattern is the pattern whose appearing times is not less than other patterns.

It is your job to find the dominating pattern(s) and their appearing times.

Input

The entire input contains multi cases. The first line of each case is an integer, which is the number of patterns N, 1N150. Each of the following N lines contains one pattern, whose length is in range [1, 70]. The rest of the case is one line contains a large string as the text to lookup, whose length is up to 106.

At the end of the input file, number `0' indicates the end of input file.

Output

For each of the input cases, output the appearing times of the dominating pattern(s). If there are more than one dominating pattern, output them in separate lines; and keep their input order to the output.

Sample Input


2
aba
bab
ababababac
6
beta
alpha
haha
delta
dede
tata
dedeltalphahahahototatalpha
0
Sample Output


4
aba
2
alpha
haha

題意:
有n個小寫字母組成的字符床和一個文本串   你的任務是找出哪些字符串在文本中出現的次數最多   例如  aba 在ababa中出現2次  但是bab只出現了一次

輸入n  之後n個字符串   長度為1-70   n小於等於150  之後一個文本串  長度最長為10的6次方   
輸出出現最多的次數 以及出現最多的字符串為什麼  如果存在多個  按輸入順序排列

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36265


思路:
很明顯的ac自動機  
[cpp]
#include<stdio.h>   #include<string.h> 
 #include<malloc.h>   #include<queue>
   #include<set>   using namespace std;
 struct node  {      int count,id; 
    struct node *next[26];    
 struct node *fail;    
 void init()      {          int i;  
       for(i=0;i<26;i++)              next[i]=NULL; 
        count=0;  
       fail=NULL;    
     id=-1;      }  }*root;
  void insert(char *str,int id)  { 
    int len,k;   
  node *p=root;
     len=strlen(str); 
    for(k=0;k<len;k++)    
 {          int pos=str[k]-'a';  
       if(p->next[pos]==NULL)          {              p->next[pos]=new node;       
      p->next[pos]->init();              p=p->next[pos];    
     }          else              p=p->next[pos]; 
    }      p->count++;   
  p->id=id;  }  void getfail()  {      int i; 
       node *p=root,*son,*temp;    
    queue<struct node *>que;   
     que.push(p);     
   while(!que.empty())         {             temp=que.front();  
          que.pop();             for(i=0;i<26;i++)      
      {                 son=temp->next[i];       
         if(son!=NULL)                 {       
             if(temp==root) {son->fail=root;}          
          else                     {                    
    p=temp->fail;                         while(p)         
               {                             if(p->next[i])         
                   {                                 son->fail=p->next[i];      
                          break;                             }                             p=p->fail;         
               }                    
    if(!p)  son->fail=root;                     }                     que.push(son);   
             }             }         }  }  int num[200];  char str[1000000+100];
 void query()  {      int len,i,cnt=0;  
   len=strlen(str);      node *p,*temp; 
    p=root;      for(i=0;i<len;i++)      {   
      int pos=str[i]-'a';      
   while(!p->next[pos]&&p!=root)  p=p->fail; 
        p=p->next[pos];//           if(!p) p=root;//   
       temp=p;    
     /*不要用*temp=*p  因為*p表示一個node,而*temp也表示一個node 但是由於*temp沒有分配空間 所以是不能進行賦值的 但是可以用temp指針去指向p*/          while(temp!=root)          {              if(temp->count>=1)              {             
    if(temp->id!=-1)                     num[temp->id]++;                    // temp->count=-1;   
           }              temp=temp->fail;    
     }      }      //printf("%d\n",cnt);
  }  char rem[160][100];  int main()  { 
    int cas,n;      while(scanf("%d",&n)!=EOF) 
    {          if(!n) break;          root=new node;   
      root->init();          root->fail=NULL;     
    int i;          getchar();         
  for(i=0;i<n;i++)          {              gets(rem[i]);   
          insert(rem[i],i);          }            getfail(); 
        memset(num,0,sizeof(num));         
  gets(str);          query();            int maxnum=-1;  
       for(i=0;i<n;i++)          {         
   // printf("num[%d]=%d\n",i,num[i]);  

            if(num[i]>maxnum) maxnum=num[i];     
    }          printf("%d\n",maxnum);       
  for(i=0;i<n;i++)              if(maxnum==num[i])  printf("%s\n",rem[i]);  
   }      return 0;  }  #include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<queue>
#include<set>
using namespace std;
struct node
{
    int count,id;
    struct node *next[26];
    struct node *fail;
    void init()
    {
        int i;
        for(i=0;i<26;i++)
            next[i]=NULL;
        count=0;
        fail=NULL;
        id=-1;
    }
}*root;
void insert(char *str,int id)
{
    int len,k;
    node *p=root;
    len=strlen(str);
    for(k=0;k<len;k++)
    {
        int pos=str[k]-'a';
        if(p->next[pos]==NULL)
        {
            p->next[pos]=new node;
            p->next[pos]->init();
            p=p->next[pos];
        }
        else
            p=p->next[pos];
    }
    p->count++;
    p->id=id;
}
void getfail()
{
    int i;
       node *p=root,*son,*temp;
       queue<struct node *>que;
       que.push(p);
       while(!que.empty())
       {
           temp=que.front();
           que.pop();
           for(i=0;i<26;i++)
           {
               son=temp->next[i];
               if(son!=NULL)
               {
                   if(temp==root) {son->fail=root;}
                   else
                   {
                       p=temp->fail;
                       while(p)
                       {
                           if(p->next[i])
                           {
                               son->fail=p->next[i];
                               break;
                           }
                           p=p->fail;
                       }
                       if(!p)  son->fail=root;
                   }
                   que.push(son);
               }
           }
       }
}
int num[200];
char str[1000000+100];
void query()
{
    int len,i,cnt=0;
    len=strlen(str);
    node *p,*temp;
    p=root;
    for(i=0;i<len;i++)
    {
        int pos=str[i]-'a';
        while(!p->next[pos]&&p!=root)  p=p->fail;
        p=p->next[pos];//
        if(!p) p=root;//
        temp=p;
        /*不要用*temp=*p  因為*p表示一個node,而*temp也表示一個node 但是由於*temp沒有分配空間 所以是不能進行賦值的 但是可以用temp指針去指向p*/
        while(temp!=root)
        {
            if(temp->count>=1)
            {
                if(temp->id!=-1)
                   num[temp->id]++;
                  // temp->count=-1;
            }
            temp=temp->fail;
        }
    }
    //printf("%d\n",cnt);
}
char rem[160][100];
int main()
{
    int cas,n;
    while(scanf("%d",&n)!=EOF)
    {
        if(!n) break;
        root=new node;
        root->init();
        root->fail=NULL;
        int i;
        getchar();

        for(i=0;i<n;i++)
        {
            gets(rem[i]);
            insert(rem[i],i);
        }

        getfail();
        memset(num,0,sizeof(num));

        gets(str);
        query();

        int maxnum=-1;
        for(i=0;i<n;i++)
        {
           // printf("num[%d]=%d\n",i,num[i]);
            if(num[i]>maxnum) maxnum=num[i];
        }
        printf("%d\n",maxnum);
        for(i=0;i<n;i++)
            if(maxnum==num[i])  printf("%s\n",rem[i]);
    }
    return 0;
}
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved