2015多校联合第八场hdu5384Danganronpa AC自动机

Posted MissZhou要努力

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2015多校联合第八场hdu5384Danganronpa AC自动机相关的知识,希望对你有一定的参考价值。

Problem Description Danganronpa is a video game franchise created and developed by Spike Chunsoft, the series' name is compounded from the Japanese words for "bullet" (dangan) and "refutation" (ronpa).

Now, Stilwell is playing this game. There are  n  verbal evidences, and Stilwell has  m  "bullets". Stilwell will use these bullets to shoot every verbal evidence.

Verbal evidences will be described as some strings  Ai , and bullets are some strings  Bj . The damage to verbal evidence  Ai  from the bullet  Bj  is  f(Ai,Bj) .
f(A,B)=i=1|A||B|+1[ A[i...i+|B|1]=B ] In other words,  f(A,B)  is equal to the times that string  B  appears as a substring in string  A .
For example:  f(ababa,ab)=2 f(ccccc,cc)=4

Stilwell wants to calculate the total damage of each verbal evidence  Ai  after shooting all  m  bullets  Bj , in other words is  mj=1f(Ai,Bj) .  
Input The first line of the input contains a single number  T , the number of test cases.
For each test case, the first line contains two integers  n m .
Next  n  lines, each line contains a string  Ai , describing a verbal evidence.
Next  m  lines, each line contains a string  Bj , describing a bullet.

T10
For each test case,  n,m105 1|Ai|,|Bj|104 |Ai|105 |Bj|105
For all test case,  |Ai|6105 |Bj|6105 Ai  and  Bj  consist of only lowercase English letters  
Output For each test case, output  n  lines, each line contains a integer describing the total damage of  Ai  from all  m  bullets,  mj=1f(Ai,Bj) .  
Sample Input
  
   1
5 6
orz
sto
kirigiri
danganronpa
ooooo
o
kyouko
dangan
ronpa
ooooo
ooooo
  
 
Sample Output
  
   1
1
0
3
7
  
 

真真是没见过这么简单的多校了,然而并不能做对orz 这个题说明了模板的重要性

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
using namespace std;
#define N 100010
#define M 26
#define X 10010

char sentencea[N][X];
char worda[X];

struct AC
    int nexta[N][M];
    int faila[N];
    int enda[N];
    int L;
    int root;

    int newnode()
        for(int i=0;i<M;i++)
            nexta[L][i]=-1;
        
        enda[L++]=0;

        return L-1;
    

    void init()
        L=0;
        root=newnode();
    

    void assert(char word[])
        int now=root;

        for(int i=0;word[i]!='\\0';i++)
            int tempch=word[i]-'a';
            if(nexta[now][tempch]==-1)
                nexta[now][tempch]=newnode();
            
            now=nexta[now][tempch];
        

        enda[now]++;
    

    void build()
        queue<int> q;

        for(int i=0;i<M;i++)
            if(nexta[root][i]==-1)
                nexta[root][i]=root;//nexta[root][i]都不存在了,哪还有什么root一直到nexta[root][i]都相等这一说,所以当然不存在fail[nexta[root][i]]?乖,回去重新比较!
            
            else
                faila[nexta[root][i]]=root;//这里nexta[root][i]已经非-1了,不需要重新赋值,但是由于nexta[root][i]是存在的所以存在一种可能从root一直到nexta[root][i]一直相等,所以好吧,给你个fail[nexta[root][i]]
                q.push(nexta[root][i]);
            
        

        while(!q.empty())
            int temp=q.front();
            q.pop();

            for(int i=0;i<M;i++)
                if(nexta[temp][i]==-1)
                    nexta[temp][i]=nexta[faila[temp]][i];
                
                else
                    faila[nexta[temp][i]]=nexta[faila[temp]][i];
                    q.push(nexta[temp][i]);
                
            
        
    

     int query(char sentence[])
        int now=root;
        long long int ans=0;

        for(int i=0;sentence[i]!='\\0';i++)
            now=nexta[now][sentence[i]-'a'];

            int temp=now;
            while(temp!=root)
                ans+=enda[temp];
                temp=faila[temp];
            
        

        return ans;
    
;

AC ac;

int main()
   // freopen("cin.txt","r",stdin);
    int t;
    int n,m;

    scanf("%d",&t);
    while(t--)
        scanf("%d%d",&n,&m);

        ac.init();
        for(int i=0;i<n;i++)
            scanf("%s",sentencea[i]);
        

        for(int i=0;i<m;i++)
            scanf("%s",worda);
            ac.assert(worda);
        
        ac.build();

        for(int i=0;i<n;i++)
            printf("%d\\n",ac.query(sentencea[i]));
        
    

    return 0;



以上是关于2015多校联合第八场hdu5384Danganronpa AC自动机的主要内容,如果未能解决你的问题,请参考以下文章

2018-2019赛季多校联合新生训练赛第八场(2018/12/22)补题题解

HDU暑假多校第八场J-Taotao Picks Apples

2019 杭电多校 第八场

航电多校第八场-A-Character Encoding

2018湖南多校第八场 训练日志

杭电多校第八场05_Separated Number(组合数前缀和性质)