【UVA】1449-Dominating Patterns(AC自己主动机)

【UVA】1449-Dominating Patterns(AC自己主动机)

大家好,又见面了,我是全栈君。

AC自己主动机的模板题。须要注意的是,对于每一个字符串,须要利用map将它映射到一个结点上,这样才干按顺序输出结果。

14360841 1449 Accepted C++ 0.146 2014-10-16 11:41:35

#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 150 * 75;
const int len  = 1111111;
const int max_size = 26;
char str[155][75];
char _str[len];
struct Trie{ //AC
    int tr[maxn][max_size];
    int fail[maxn];
    int val[maxn];     //结果
    int sz,root,_max;  //出现次数
    map<int,int>countx;//计算第i个字符串出现的次数
    map<string,int>vis;
    int index(char c){
        return c - 'a';
    }
    int newnode(){
        val[sz] = 0;
        for(int i = 0; i < 26; i++) tr[sz][i] = -1;
        sz ++;
        return sz - 1;
    }
    void init(){
        sz = 0;_max = 0;
        root = newnode();
        countx.clear();
        vis.clear();
    }
    void insert(char *s,int id){
         int u = root;
         int n = strlen(s);
         for(int i = 0; i < n; i++){
              int c = index(s[i]);
              //printf("%d %d\n",u,c);
              if(tr[u][c] == -1)
                   tr[u][c] = newnode();
              u = tr[u][c];
         }
         vis[string(s)] = u; //这个字符串相应的结点编号
         //printf("%d\n",u);
         val[u] ++;//达到这个结点的单词
    }
    void build(){
        queue<int>q;
        fail[root] = root;
        for(int i = 0; i < 26; i++){
            if(tr[root][i] == -1)
               tr[root][i] = root;
            else{
                fail[tr[root][i]] = root;
                q.push(tr[root][i]);
            }
        }
        while(!q.empty()){
            int now = q.front(); q.pop();
            for(int i = 0; i < 26; i++){
                if(tr[now][i] == -1)
                    tr[now][i] = tr[fail[now]][i];
                else{
                    fail[tr[now][i]] = tr[fail[now]][i];
                    q.push(tr[now][i]);
                }
            }
        }
    }
    void countt(char *s){
        int n = strlen(s);
        int u = root;
        for(int i = 0; i < n; i++){
            u = tr[u][index(s[i])];
            int temp = u;
            while(temp != root){
                if(val[temp]){           //这个结点存在单词
                    countx[temp]++;      //这个结点
                    _max = max(_max,countx[temp]);
                }
                temp = fail[temp];
            }
        }
    }
};
Trie ac;
int main(){
    int n;
    while(scanf("%d",&n) && n){
        ac.init();
        for(int i = 0; i < n; i++){
            scanf("%s",str[i]);
            ac.insert(str[i],i); //插入单词
        }
        //printf("%d\n",ac.sz);
        ac.build();
        scanf("%s",_str);
        ac.countt(_str);
        printf("%d\n",ac._max);
        for(int i = 0; i < n; i++){
            if(ac.countx[ac.vis[string(str[i])]] == ac._max)
               printf("%s\n",str[i]);
        }
    }
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115847.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Python变量的命名_python函数命名规则

    Python变量的命名_python函数命名规则一、变量的概念变量名只有在第一次出现的时候,才是定义变量。当再次出现时,不是定义变量,而是直接使用之前定义的变量。1.变量命名1)命名的规范性变量名可以包括字母、数字、下划线,但是数字不能做为开头。例如:name1是合法变量名,而1name就不可以。 系统关键字不能做变量名使用 除了下划线之个,其它符号不能做为变量名使用 Python的变量名是除分大小写的2)驼峰命名法…

    2022年5月3日
    53
  • mysql联合索引的使用规则

    mysql联合索引的使用规则从一道有趣的题目开始分析:假设某个表有一个联合索引(c1,c2,c3,c4)以下选项哪些字段使用了该索引:Awherec1=xandc2=xandc4>xandc3=xBwherec1=xandc2=xandc4=xorderbyc3Cwherec1=xandc4=xgroupbyc3,c2Dwherec1=?andc5

    2022年6月4日
    54
  • numpy.astype数据精度导致数据变化的问题「建议收藏」

    numpy.astype数据精度导致数据变化的问题「建议收藏」在用numpy.astype强制转换数据类型的时候,由于numpy精度的问题将会对长度超过16位的数据发生不可预见的变化。见以下样例:a=np.random.randint(10000000000000000,100000000000000000,6,dtype=np.int64).reshape(3,-1)aOut[250]:array([[84627891441616237…

    2022年5月2日
    73
  • allure生成的报告怎么查看_allure测试报告

    allure生成的报告怎么查看_allure测试报告原因可能由于:因为pytest-allure-adaptor库基本被python3放弃了,运行很不友好,所以报错解决方法:terminal中先卸载:pipuninstallpytest-allure-adaptor再安装:pipallure-pytest若安装找不到该模块可以去设置那下载…

    2022年9月20日
    2
  • navicat premium 15 激活码[免费获取]

    (navicat premium 15 激活码)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~M…

    2022年3月20日
    127
  • 普罗米修斯监控系统_一步步教你用Prometheus搭建实时监控系统系列(二)——详细分析拉取和推送两种不同模式…

    普罗米修斯监控系统_一步步教你用Prometheus搭建实时监控系统系列(二)——详细分析拉取和推送两种不同模式…前言本系列着重介绍Prometheus以及如何用它和其周边的生态来搭建一套属于自己的实时监控告警平台。本系列受众对象为初次接触Prometheus的用户,大神勿喷,偏重于操作和实战,但是重要的概念也会精炼出提及下。系列主要分为以下几块Prometheus各个概念介绍和搭建,如何抓取数据(一步步教你用Prometheus搭建实时监控系统系列(一)——上帝之火,普罗米修斯的崛起)如何推送数据…

    2022年7月19日
    24

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号