zoukankan      html  css  js  c++  java
  • hdu 2896 病毒侵袭 (AC)

    http://acm.hdu.edu.cn/showproblem.php?pid=2896

    这题WA了好多好多次,大概8、9次吧,从cf进行到一半就开始在这调。经过不懈的努力终于发现,TMD,vis的memset放错地方了!!!这都什么事啊,老被这种错给整的那么郁闷。。。

    code:

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std ;
    const int kind = 130;
    struct node{
        node *fail ;
        node *next[kind] ;
        int count ;
        int id ;
        node(){
           fail = NULL ;
           count = 0 ;
           memset(next, NULL, sizeof(next)) ;
       }
    }*q[500001] ;
    char keyword[201] ;
    char str[10001] ;
    int head, tail,ID ;
    bool vis[501] ;

    void insert(char *str, node *root){
        node *p = root ;
        int i = 0, index ;
        while(str[i]){
            index = str[i] ;
            if(p->next[index]==NULL) p->next[index]=new node() ;
            p = p->next[index] ;
            i ++ ;
        }
        p->count ++ ;
        p->id = ID ++ ;
    }
    void build_ac_automation(node *root){
        int i ;
        root->fail = NULL ;
        q[head++] = root ;
        while(head!=tail){
            node *temp = q[tail++] ;
            node *p = NULL ;
            for(i=0; i<130; i++){
                if(temp->next[i]!=NULL){
                    if(temp==root) temp->next[i]->fail = root ;
                    else{
                        p = temp->fail ;
                        while(p!=NULL){
                            if(p->next[i]!=NULL){
                                temp->next[i]->fail = p->next[i] ;
                                break ;
                            }
                            p = p->fail ;
                        }
                        if(p==NULL) temp->next[i]->fail = root ;
                    }
                    q[head++] = temp->next[i] ;
                }
            }
        }
    }
    int query(node *root){
        int i = 0, cnt = 0, index ;
        node *p = root ;
        while(str[i]){
            index = str[i] ;
            while(p->next[index]==NULL && p!=root)
                p = p->fail ;
            p = p->next[index] ;
            p = (p==NULL) ? root : p ;
            node *temp = p ;
            while(temp!=root&&temp->count==1){
                cnt += temp->count ;
                vis[temp->id] = true ;
                temp = temp->fail ;
            }
            i ++ ;
        }
        return cnt ;
    }
    int main(){
        int n, m, sum = 0 ;
        ID = 1 ;
        scanf("%d", &n) ;
        head = tail = 0 ;
        node *root = new node() ;
        for(int i=0; i<n; i++){
            getchar() ;
            scanf("%s", keyword) ;
            insert(keyword, root) ;
        }
        scanf("%d", &m) ;
        build_ac_automation(root) ;
        for(int i=1; i<=m; i++){
            memset(vis, falsesizeof(vis)) ;
            getchar() ;
            scanf("%s", str) ;
            if(query(root)){
                sum ++ ;
                printf("web %d:", i) ;
                for(int j=1; j<=n; j++)
                    if(vis[j])  printf(" %d", j) ;
                printf("\n") ;
            }
        }
        printf("total: %d\n", sum) ;
        return 0;}
  • 相关阅读:
    windows无法完成格式化
    javascript数组添加元素的三种方式
    sql server将字符串类型转换为数值类型
    javascript将字符串转化成json对象的3种方法
    bootstrap table刷新表格记录
    html元素的title属性值换行
    webstorm快捷键失效问题
    设计模式之原型,学习笔记
    设计模式之封装,学习笔记
    初了解JS设计模式,学习笔记
  • 原文地址:https://www.cnblogs.com/xiaolongchase/p/2360764.html
Copyright © 2011-2022 走看看