• hdu 5384 Danganronpa


    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
     
    Recommend
    wange2014   |   We have carefully selected several similar problems for you:  6447 6446 6445 6444 6443 
    给出n个A串和m个B串,对于每个A串,输出里面出现了几次B串,ac自动机对所有B串构建字典树,然后进行匹配查找。
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #define MAX 100001
    using namespace std;
    
    int pos;
    int trie[MAX][26],fail[MAX] = {-1},sum[MAX];
    char s[100001][10001];
    void Insert_Str(char *s) {///字符串插入到字典树中
       int i = -1,r = 0;
       while(s[++ i]) {
           int d = s[i] - 'a';
           if(!trie[r][d]) {
               trie[r][d] = ++ pos;
           }
           r = trie[r][d];
       }
       sum[r] ++;
    }
    void Build_Fail() {///通过父结点的Fail更新子结点的Fail
        queue<int> q;
        q.push(0);
        while(!q.empty()) {
            int id = q.front();
            q.pop();
            for(int i = 0;i < 26;i ++) {
                if(trie[id][i]) {///第i个儿子存在
                    int temp = fail[id];///temp赋值当前节点的Fail
                    while(temp != -1) {
                        if(trie[temp][i]) {
                            fail[trie[id][i]] = trie[temp][i];
                            break;
                        }
                        temp = fail[temp];
                    }
                    q.push(trie[id][i]);
                }
            }
        }
    }
    void Ac_automation(int k) {
        int i = -1,r = 0,ans = 0;
        while(s[k][++ i]) {
            int d = s[k][i] - 'a';
            while(r && !trie[r][d]) r = fail[r];///如果没有匹配的子结点 就找它的Fail看看有没有匹配的子结点
            if(trie[r][d]) r = trie[r][d];
            int temp = r;
            while(temp) {
                ans += sum[temp];
                temp = fail[temp];///找最长后缀
            }
        }
        printf("%d
    ",ans);
    }
    int main() {
        char str[10001];
        int t,n,m;
        scanf("%d",&t);
        while(t --) {
            pos = 0;
            memset(trie,0,sizeof(trie));
            memset(fail + 1,0,sizeof(fail));
            memset(sum,0,sizeof(sum));
            scanf("%d %d",&n,&m);
            for(int i = 0;i < n;i ++) {
                scanf("%s",s[i]);
            }
            for(int i = 0;i < m;i ++) {
                scanf("%s",str);
                Insert_Str(str);
            }
            Build_Fail();
            for(int i = 0;i < n;i ++) {
                Ac_automation(i);
            }
        }
        return 0;
    }
  • 相关阅读:
    【转】Struts2 和 Spring MVC对比
    【转】JVM介绍
    linux内核阻塞IO
    linux并发concurrency控制
    内核延时
    linux中断编程
    (转)关于java.lang.UnsupportedClassVersionError解决方法总结
    (转) 使用jdk的xjc命令由schema文件生成相应的实体类
    TWS日志查看
    (转)IBM MQ 创建以及常见问题集锦
  • 原文地址:https://www.cnblogs.com/8023spz/p/9798811.html
Copyright © 2020-2023  润新知