【UVA】1449-Dominating Patterns(AC自己主动机)
2024-10-20 21:13:27
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;
}
最新文章
- mysql存储过程中 乱码问题解决办法
- python走起之第九话
- [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:为ASP.NET MVC应用程序实现继承
- 图文安装Windows Template Library - WTL Version 9.0
- REST签名认证
- JQ+rotate插件实现图片旋转,兼容IE7+ \ CHROME等浏览器
- UVA 11419 SAM I AM(最大二分匹配&;最小点覆盖:K&#246;nig定理)
- Android 如何切换到 Transform API?
- iOS微信支付
- 使用NSTimer实现倒计时-备
- (二)CSS3应用 - 实现圆角
- Flex疑难小杂症
- java 服务
- webpack学习笔记(二)-- 初学者常见问题及解决方法
- python制作爬虫爬取京东商品评论教程
- ajax知识点
- git下载指定的版本
- 网络编程学习笔记:Socket编程
- Centos7安装SVN服务器
- Spring 配置文件
热门文章
- 图片旋转+剪裁js插件(兼容各浏览器) « 张鑫旭-鑫空间-鑫生活
- [Oracle] Data Guard 系列(5) - 创建逻辑备库
- LeetCode——Reverse Words in a String
- 2014年百度之星资格赛第三题Xor Sum
- Android应用程序窗口(Activity)的运行上下文环境(Context)的创建过程分析
- windows下搭建及配置mantis缺陷管理工具
- CMD下用csc.exe编译.cs 代码
- angular controller之间通信方式
- <;httpRuntime>;
- CentOS 7 启动VNC失败问题