【UVA】1449-Dominating Patterns(AC自己主动机)
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自己主动机)的更多相关文章
- UVa 1449 - Dominating Patterns (AC自动机)
题目大意:给出多个字符串模板,并给出一个文本串,求在文本串中出现最多的模板,输出最多的次数并输出该模板(若有多个满足,则按输入顺序输出). 思路:赤裸裸的 AC自动机,上模板. 代码: #includ ...
- 【UVA】11468-Substring(AC自己主动机)
AC自己主动机的题,须要注意的,建立失配边的时候,假设结点1失配边连到的那个结点2,那个结点2是一个单词的结尾,那么这个结点1也须要标记成1(由于能够看成,这个结点包括了这个单词),之后在Trie树上 ...
- uva 1449 - Dominating Patterns
简单的AC自动机: #include<cstdio> #include<cstring> #include<queue> #define maxn 150005 u ...
- UVA 10679 I love Strings!!!(AC自己主动机)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- uva 11468 - Substring(AC自己主动机+概率)
题目链接:uva 11468 - Substring 题目大意:给出一些字符和各自字符相应的选择概率.随机选择L次后得到一个长度为L的字符串,要求该字符串不包括随意一个子串的概率. 解题思路:构造AC ...
- POJ 2778 DNA Sequence (AC自己主动机 + dp)
DNA Sequence 题意:DNA的序列由ACTG四个字母组成,如今给定m个不可行的序列.问随机构成的长度为n的序列中.有多少种序列是可行的(仅仅要包括一个不可行序列便不可行).个数非常大.对10 ...
- hdu 2222 Keywords Search ac自己主动机
点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- POJ 3691 & 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: ...
- HDU 2896 病毒侵袭 AC自己主动机题解
本题是在text里面查找key word的增强版.由于这里有多个text. 那么就不能够简单把Trie的叶子标志记录改动成-1进行加速了,能够使用其它技术.我直接使用个vis数组记录已经訪问过的节点, ...
随机推荐
- 456. 132 Pattern
456. 132 Pattern Given an array of integers a1, a2, a3-an, judge if there exists the 132 pattern. 13 ...
- Capture the Flag(模拟)
Capture the Flag Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge In computer se ...
- 代码中实际运用memcached——java
以下文章取自:http://jameswxx.iteye.com/blog/1168711 memcached的java客户端有好几种,http://code.google.com/p/memcach ...
- iOS UIScrollView的简单使用
本文代码下载 http://vdisk.weibo.com/s/BDn59yfnBVMAJ // // ViewController.m // ScrollView_T1119 // // Creat ...
- 由闭包引起的对javascript代码可维护性的思考
在最近的编程实践中由闭包的使用引起了我对javascript代码可维护性的思考.面向对象的其中一个特性封装性通过封装可以降低类与类之间或模块与模块之间耦合性从而使我们的设计更加高内聚低耦合,在大规模的 ...
- 在Linux系统上限制远程登录的IP
在Linux系统上限制远程登录的IP,使用系统自带的配置文件. /etc/hosts.allow /etc/hosts.deny 匹配原则 先allow 后deny.
- PHP调用WCF小结
新工作第三周,做了3年多的.Net,突然急转弯做PHP,漂移过弯,速度180迈 由于数据的整合,在项目中不得不使用PHP调用WCF 一头的雾水,网上相关的资料少又少,在phpChina发个帖子,还没有 ...
- <runtime> 的 <assemblyBinding> 元素
一.<assemblyBinding> 元素 包含有关程序集版本重定向和程序集位置的信息. <assemblyBinding xmlns="urn:schemas-micr ...
- ARCGIS接口详细说明
ArcGIS接口详细说明 目录 ArcGIS接口详细说明... 1 1. IField接口(esriGeoDatabase)... 2 2. IFieldEdit接口(esriGe ...
- Winform获取应用程序的当前路径的方法集合(转)
Winform获取应用程序的当前路径的方法集合,具体如下,值得收藏 //获取当前进程的完整路径,包含文件名(进程名). string str = this.GetType().Assembly.Loc ...