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

14360841 1449

option=com_onlinejudge&Itemid=8&page=show_problem&problem=4195" style="font-size:13.3333330154419px; margin:0px; padding:0px; color:rgb(153,0,0); text-decoration:none">Dominating Patterns

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;
}

【UVA】1449-Dominating Patterns(AC自己主动机)的更多相关文章

  1. UVa 1449 - Dominating Patterns (AC自动机)

    题目大意:给出多个字符串模板,并给出一个文本串,求在文本串中出现最多的模板,输出最多的次数并输出该模板(若有多个满足,则按输入顺序输出). 思路:赤裸裸的 AC自动机,上模板. 代码: #includ ...

  2. 【UVA】11468-Substring(AC自己主动机)

    AC自己主动机的题,须要注意的,建立失配边的时候,假设结点1失配边连到的那个结点2,那个结点2是一个单词的结尾,那么这个结点1也须要标记成1(由于能够看成,这个结点包括了这个单词),之后在Trie树上 ...

  3. uva 1449 - Dominating Patterns

    简单的AC自动机: #include<cstdio> #include<cstring> #include<queue> #define maxn 150005 u ...

  4. UVA 10679 I love Strings!!!(AC自己主动机)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  5. uva 11468 - Substring(AC自己主动机+概率)

    题目链接:uva 11468 - Substring 题目大意:给出一些字符和各自字符相应的选择概率.随机选择L次后得到一个长度为L的字符串,要求该字符串不包括随意一个子串的概率. 解题思路:构造AC ...

  6. POJ 2778 DNA Sequence (AC自己主动机 + dp)

    DNA Sequence 题意:DNA的序列由ACTG四个字母组成,如今给定m个不可行的序列.问随机构成的长度为n的序列中.有多少种序列是可行的(仅仅要包括一个不可行序列便不可行).个数非常大.对10 ...

  7. hdu 2222 Keywords Search ac自己主动机

    点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  8. POJ 3691 &amp; HDU 2457 DNA repair (AC自己主动机,DP)

    http://poj.org/problem?id=3691 http://acm.hdu.edu.cn/showproblem.php?pid=2457 DNA repair Time Limit: ...

  9. HDU 2896 病毒侵袭 AC自己主动机题解

    本题是在text里面查找key word的增强版.由于这里有多个text. 那么就不能够简单把Trie的叶子标志记录改动成-1进行加速了,能够使用其它技术.我直接使用个vis数组记录已经訪问过的节点, ...

随机推荐

  1. Kindeditor+web.py+SAE Storage 实现文件上传 - 开源中国社区

    Kindeditor+web.py+SAE Storage 实现文件上传 - 开源中国社区 Kindeditor+web.py+SAE Storage 实现文件上传

  2. PHP安装和配置

    编译安装php5-5.2.6-49.11.src.rpm: ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache/bin ...

  3. Vitaliy and Pie(模拟)

    Vitaliy and Pie Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Su ...

  4. easy ui 如何单个引用其中某个插件?

    记录一下这个方法,前端时间一直在纠结这个问题,后来听一些前辈讲解后才恍然大悟,要单独引用某个插件,我们需要重视的是:easyloaer.js ,easy ui的下载包中也有easyloader的dem ...

  5. 【DataStructure】Some useful methods about linkedList(二)

    Method 1: Add one list into the other list. For example, if list1is {22, 33, 44, 55} and  list2 is { ...

  6. poj2503--Babelfish(特里一水)

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 32988   Accepted: 14189 Descr ...

  7. CSDN-Markdown语法集锦

    前言: 使用Markdown近一个来月.越来越认为不舒爽. 改字体.改字号.改颜色.改样式,全不会!想加个数学公式.得,仅仅会截图.把图片传上去了还不会控制大小.也不会控制文字与图片的排版,写出来的博 ...

  8. Qt 信号与槽

    Qt信号与槽的理解 信号和槽机制是 QT 的核心机制,要精通 QT 编程就必须对信号和槽有所了解.信号和槽是一种高级接口,应用于对象之间的通信,它是 QT 的核心特性,也是 QT 区别于其它工具包的重 ...

  9. Flashback Version/Transaction Query

    1.应用Flashback Version Query查询记修改版本 SQL> select dbms_flashback.get_system_change_number from dual; ...

  10. Oracle 中的Userenv()

    1.USEREVN() 返回当前用户环境的信息,opt可以是:ENTRYID,SESSIONID,TERMINAL,ISDBA,LABLE,LANGUAGE,CLIENT_INFO,LANG,VSIZ ...