HDU 5384 Danganronpa

Danganronpa

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 743    Accepted Submission(s): 402


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
 

Author
SXYZ
 

Source
2015 Multi-University Training Contest 8


AC自动机模板题。有一点要注意:对于A组每一个字符串,最后的答案是B组所有匹配到的相加。比如对于A的"ooooo",应当是将B里"00000"(2次),"0"(5次)相加。总共7次。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define clr(x,y) memset(x,y,sizeof(x))
using namespace std;

const int maxnode = 600010;
const int SIGMA_SIZE = 26;
const int maxl = 100005;
char A[maxl][10005],P[maxl];
int sz;
int ch[maxnode][SIGMA_SIZE];
int val[maxnode],f[maxnode];


void init(){ sz=1;clr(ch[0],0); clr(val,0); }

void insert(char* s){
    int n = strlen(s);
    int u = 0;
    for(int i = 0; i < n; i++){
        int c = s[i] - 'a';
        if (!ch[u][c]){
            clr(ch[sz], 0);
            val[sz] = 0;
            ch[u][c] = sz++;
        }
        u = ch[u][c];
    }
    val[u]++;
}

void getFail(){
    queue<int> Q;
    f[0] = 0;
    for(int c = 0; c < SIGMA_SIZE; c++){
        int u = ch[0][c];
        if (u) { Q.push(u); f[u] = 0; }
    }
    while(!Q.empty()){
        int r = Q.front(); Q.pop();
        for(int c = 0; c < SIGMA_SIZE; c++){
            int u = ch[r][c];
            if (!u) { ch[r][c] = ch[f[r]][c]; continue;}
            f[u] = ch[f[r]][c];
            Q.push(u);
        }
    }
}

int solve(char* s){
    int n = strlen(s);
    int u = 0, ans = 0;
    for(int i = 0; i < n; i++){
        int c = s[i] - 'a';
        u = ch[u][c];
        int v = u;
        while(v) { ans += val[v];
        v = f[v]; }
    }
    return ans;
}
int main()
{
    int t; scanf("%d",&t);
    while(t--){
        init();
        int n,m; scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
                scanf("%s",A[i]);
        for(int i=0;i<m;i++){ scanf("%s",P); insert(P);}
        getFail();
        for(int i=0;i<n;i++)
                printf("%d\n",solve(A[i]));
    }
    return 0;
}


 

你可能感兴趣的:(HDU 5384 Danganronpa)