题意:

  就是求是否有一个串 是其它所有串的母串

解析:

  把所有的串都加入到trie数中  然后用最长的串去匹配就好了

emm。。开始理解错题意了。。。看成了只要存在一个串是另一个的母串就好。。

然后输出这样的串。。。用后缀数组写完后。。。才发现  理解错了 emm。。

指针的会超时emm。。。然后才学了 用数组写

#include<stdio.h>
#include<string.h>
#include<queue>
#include<string>
#include<iostream>
#define maxn 1000010
using namespace std;
int n;
int nxt[maxn][],fail[maxn],edd[maxn],root,L;//nxt记录节点,在这里edd指针代表以当前节点为字符串尾的字符串个数
int vis[maxn];
int newnode()
{
for(int i=;i<;i++)
nxt[L][i]=-;//节点连接的边初始化为-1
edd[L]=;
vis[L]=;
return L++;
}
void init()
{
L=;
root=newnode();
} void insert(char *str)//trie树的建立
{
int l = strlen(str);
int now=root;
for(int i=;i<l;i++)
{
int x = str[i] - 'a';
if(nxt[now][x]==-)nxt[now][x]=newnode();
now=nxt[now][x];
}
edd[now]++;
}
void build()//建立ac自动机
{
queue<int> Q;
for(int i=;i<;i++)
{
if(nxt[root][i]==-)nxt[root][i]=root;
else //若有连边则将节点加入队列 ,并将fail指针指向root
{
fail[nxt[root][i]]=root;
Q.push(nxt[root][i]);
}
}
while(!Q.empty())
{
int now=Q.front();
Q.pop();
for(int i=;i<;i++)
{
if(nxt[now][i]==-) //若无连边,则将该边指向当前节点fail指针指向的相应字符连接的节点
nxt[now][i]=nxt[fail[now]][i];
else //若有连边,则将儿子节点的fail指针指向当前节点fail指针指向相应字符接的节点
{
fail[nxt[now][i]]=nxt[fail[now]][i];
Q.push(nxt[now][i]); //加入队列继续遍历
}
}
}
}
int query(char str[])
{
int l = strlen(str);
int now=root;
int res=;
for(int i=;i<l;i++)
{
int x = str[i] - 'a';
now=nxt[now][x];
int temp=now;
while(temp!=root&&vis[temp]==)//根据题目要求改变形式
{
res+=edd[temp];
edd[temp]=;
vis[temp]=;
temp=fail[temp];
}
}
return res; //在这里返回的是匹配到的模式串的数量
}
char str[maxn], ans[maxn];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
init();
int ma=;
for(int i=;i<n;i++)
{
scanf("%s",str);
int l=strlen(str);
if(ma<l)
{
ma=l;
strcpy(ans,str);
}
insert(str);
}
build();
int sum=query(ans);
if(sum==n) puts(ans);
else puts("No");
}
}

The Dominator of Strings HDU - 6208(ac自动机板题)的更多相关文章

  1. HDU - 6208 The Dominator of Strings HDU - 6208 AC自动机 || 后缀自动机

    https://vjudge.net/problem/HDU-6208 首先可以知道最长那个串肯定是答案 然后,相当于用n - 1个模式串去匹配这个主串,看看有多少个能匹配. 普通kmp的话,每次都要 ...

  2. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  3. HDU 3065 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...

  4. HDU 2896 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...

  5. hdu 2222(AC自动机模版题)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  6. 2017 ACM/ICPC Asia Regional Qingdao Online 1003 The Dominator of Strings hdu 6208

    The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java ...

  7. HDU 2222 AC自动机 裸题

    题意: 问母串中出现多少个模式串 注意ac自动机的节点总数 #include <stdio.h> #include <string.h> #include <queue& ...

  8. Keywords Search HDU - 2222(ac自动机板题。。)

    求一个字符串上有多少个匹配的单词 看着卿学姐的板子写的 指针形式: #include <iostream> #include <cstdio> #include <sst ...

  9. HDU 2222 AC自动机模版题

    所学的AC自动机都源于斌哥和昀神的想法. 题意:求目标串中出现了几个模式串. 使用一个int型的end数组记录,查询一次. #include <cstdio> #include <c ...

随机推荐

  1. ubuntu下安装pip install mysqlclient 报错 command "python setup.py egg_info" failed with error.....解决方案

    我的环境: ubuntu 1604 版本, 在黑屏终端已经安装了django和virtualenv虚拟环境, 在创建了django的models后开始迁移的操作, 出现错误, 错误代码最后如题目 可以 ...

  2. Jmeter接口测试(七)用例数据分离

    之前我们的用例数据都是配置在 Jmeter Http 请求中,每次需要增加,修改用例都需要打开 jmeter 重新编辑,当用例越来越多的时候,用例维护起来就越来越麻烦,有没有好的方法来解决这种情况呢? ...

  3. Netty源码分析第4章(pipeline)---->第7节: 前章节内容回顾

    Netty源码分析第四章: pipeline 第七节: 前章节内容回顾 我们在第一章和第三章中, 遗留了很多有关事件传输的相关逻辑, 这里带大家一一回顾 首先看两个问题: 1.在客户端接入的时候, N ...

  4. WebRtc与SIP

    最近研究一下 webrtc ,看了几篇paper,之前也尝试运行验证了几个demo,现在把我的理解总结到这里. WebRTC 简介 WebRTC,名称源自网页实时通信(Web Real-Time Co ...

  5. ftp部署及使用

    常用软件安装及使用目录 http://www.jb51.net/article/106604.htm   ftp部署 本篇文章主要介绍了CentOS7.0下安装FTP服务的方法,小编觉得挺不错的,现在 ...

  6. django之基本配置

    Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能. ...

  7. LIFI热火下的VLC基本链路、标准及发展问题

    和白炽及荧光灯相比,白光发光二极管(LED)具有寿命长.光效高.功耗低.无辐射.安全性好.可靠性高等特点,被称为"绿色照明"并得到迅猛发展.白光LED在未来市场极具竞争力.世界范围 ...

  8. 关于注册github

  9. Ubuntu登录界面添加root用户登录选项

    1.普通用户登录系统并打开终端 配置root密码 $sudo passwd 切换至root用户 $su root 输入密码 修改以下配置文件 $nano /usr/share/lightdm/ligh ...

  10. pktgen-dpdk 实战

    官方手册:http://pktgen-dpdk.readthedocs.io/en/latest/getting_started.html 过程 开机(重启) 把DPDK那一套流程走一遍(环境变量设置 ...