【暑假】[实用数据结构]UVAlive 4670 Dominating Patterns
UVAlive 4670 Dominating Patterns
题目:
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description

The archaeologists are going to decipher a very mysterious ``language". Now, they know many language patterns; each pattern can be treated as a string on English letters (only lower case). As a sub string, these patterns may appear more than one times in a large text string (also only lower case English letters).
What matters most is that which patterns are the dominating patterns. Dominating pattern is the pattern whose appearing times is not less than other patterns.
It is your job to find the dominating pattern(s) and their appearing times.
Input
The entire input contains multi cases. The first line of each case is an integer, which is the number of patterns N, 1N
150. Each of the following N lines contains one pattern, whose length is in range [1, 70]. The rest of the case is one line contains a large string as the text to lookup, whose length is up to 106.
At the end of the input file, number `0' indicates the end of input file.
Output
For each of the input cases, output the appearing times of the dominating pattern(s). If there are more than one dominating pattern, output them in separate lines; and keep their input order to the output.
Sample Input
2
aba
bab
ababababac
6
beta
alpha
haha
delta
dede
tata
dedeltalphahahahototatalpha
0
Sample Output
4
aba
2
alpha
haha
思路:
题目给出一个文本串多个模板串,要求出现最多的模板串。这恰好可以用AC自动机解决,只不过需要将print修改为cnt[val]++ 统计标号为val的模板串出现的次数。
原理:在文本串不同位置出现的模板都可以通过自动机匹配找到。
注意:为什么模板要开始从1标号? : 因为调用了insert(word[i],i)语句,如果给模板标号0的话相当于舍弃了这个模板串(val==0代表非单词结点),因此调用AhoCorasickaotomata的时候一定要注意不能把单词结点的val设为0。
代码:
这里给出三份AC代码:
无去重
#include<cstdio>
#include<cstring>
#include<queue>
#include<map>
#include<string>
using namespace std; const int maxl = + ;
const int maxw = + ;
const int maxwl = + ;
const int sigma_size = ; struct AhoCorasickaotomata{
int ch[maxl][sigma_size];
int val[maxl];
int cnt[maxw]; //计数
int f[maxl];
int last[maxl];
int sz; void clear(){
sz=;
memset(ch[],,sizeof(ch[]));
memset(cnt,,sizeof(cnt));
}
int ID(char c) { return c-'a'; } void insert(char* s,int v){
int u= , n=strlen(s);
for(int i=;i<n;i++){
int c=ID(s[i]);
if(!ch[u][c]) { //if ! 初始化结点
memset(ch[sz],,sizeof(ch[sz]));
val[sz]=;
ch[u][c]= sz++;
}
u=ch[u][c];
}
val[u]=v;
} void print(int j){
if(j){ //递归结尾
cnt[val[j]] ++;
print(last[j]);
}
}
void find(char* s){
int n=strlen(s);
int j=;
for(int i=;i<n;i++){
int c=ID(s[i]);
while(j && !ch[j][c]) j=f[j];
//沿着失配边寻找与接下来一个字符可以匹配的字串
j=ch[j][c];
if(val[j]) print(j);
else if(last[j]) print(last[j]);
}
} void getFail() {
queue<int> q;
f[]=;
for(int i=;i<sigma_size;i++){ //以0结点拓展入队
int u=ch[][i];
if(u) { //u存在
q.push(u); f[u]=; last[u]=;
}
}
//按照BFS熟悉构造失配 f & last
while(!q.empty()){
int r=q.front(); q.pop();
for(int i=;i<sigma_size;i++){
int u=ch[r][i];
if(!u) continue; //本字符不存在
q.push(u);
int v=f[r];
while(v && !ch[v][i]) v=f[v]; //与该字符匹配
v=ch[v][i]; //相同字符的序号
f[u]=v;
last[u] = val[v]? v : last[v];
//递推 last
//保证作为短后缀的字串可以匹配
}
}
}
}; AhoCorasickaotomata ac;
char T[maxl]; int main(){
int n;
while(scanf("%d",&n)== && n){
char word[maxw][maxwl];
ac.clear(); //operation 1 //init
int x=n;
for(int i=;i<=n;i++){ //i 从 1 开始到 n
scanf("%s",word[i]);
ac.insert(word[i],i);
}
ac.getFail(); //operation 2
scanf("%s",T);
int L=strlen(T);
ac.find(T); //operation 3
int best = -;
for(int i=;i<=n;i++) best=max(best,ac.cnt[i]);
printf("%d\n",best);
for(int i=;i<=n;i++)
if(ac.cnt[i] == best) printf("%s\n",word[i]);
}
return ;
}
时间:46 ms
+map处理
我的代码:
#include<cstdio>
#include<cstring>
#include<queue>
#include<map>
#include<string>
using namespace std; const int maxl = + ;
const int maxw = + ;
const int maxwl = + ;
const int sigma_size = ; struct AhoCorasickaotomata{
int ch[maxl][sigma_size];
int val[maxl];
int cnt[maxw]; //计数
int f[maxl];
int last[maxl];
int sz;
map<string,int> ms; //对string打标记 避免重复 void clear(){
sz=;
memset(ch[],,sizeof(ch[]));
memset(cnt,,sizeof(cnt));
ms.clear();
}
int ID(char c) { return c-'a'; } void insert(char* s,int v){
int u= , n=strlen(s);
for(int i=;i<n;i++){
int c=ID(s[i]);
if(!ch[u][c]) { //if ! 初始化结点
memset(ch[sz],,sizeof(ch[sz]));
val[sz]=;
ch[u][c]= sz++;
}
u=ch[u][c];
}
val[u]=v;
} void print(int j){
if(j){ //递归结尾
cnt[val[j]] ++;
print(last[j]);
}
}
void find(char* s){
int n=strlen(s);
int j=;
for(int i=;i<n;i++){
int c=ID(s[i]);
while(j && !ch[j][c]) j=f[j];
//沿着失配边寻找与接下来一个字符可以匹配的字串
j=ch[j][c];
if(val[j]) print(j);
else if(last[j]) print(last[j]);
}
} void getFail() {
queue<int> q;
f[]=;
for(int i=;i<sigma_size;i++){ //以0结点拓展入队
int u=ch[][i];
if(u) { //u存在
q.push(u); f[u]=; last[u]=;
}
}
//按照BFS熟悉构造失配 f & last
while(!q.empty()){
int r=q.front(); q.pop();
for(int i=;i<sigma_size;i++){
int u=ch[r][i];
if(!u) continue; //本字符不存在
q.push(u);
int v=f[r];
while(v && !ch[v][i]) v=f[v]; //与该字符匹配
v=ch[v][i]; //相同字符的序号
f[u]=v;
last[u] = val[v]? v : last[v];
//递推 last
//保证作为短后缀的字串可以匹配
}
}
}
}; AhoCorasickaotomata ac;
char T[maxl]; int main(){
int n;
while(scanf("%d",&n)== && n){
char word[maxw][maxwl];
ac.clear(); //operation 1 //init
int x=n;
for(int i=;i<=n;i++){ //i 从 1 开始到 n
scanf("%s",word[i]);
if(!ac.ms.count(word[i])){
ac.insert(word[i],i);
ac.ms[string(word[i])] =i; //string(char[])=>string
}
else x--; //改变长度
}
n=x; //n为去重之后的长
ac.getFail(); //operation 2
scanf("%s",T);
int L=strlen(T);
ac.find(T); //operation 3
int best = -;
for(int i=;i<=n;i++) best=max(best,ac.cnt[i]);
printf("%d\n",best);
for(int i=;i<=n;i++)
if(ac.cnt[i] == best) printf("%s\n",word[i]);
}
return ;
}
Code 1:我的代码
时间:49 ms
作者代码:
// LA4670 Dominating Patterns
// Rujia Liu
#include<cstring>
#include<queue>
#include<cstdio>
#include<map>
#include<string>
using namespace std; const int SIGMA_SIZE = ;
const int MAXNODE = ;
const int MAXS = + ; map<string,int> ms; struct AhoCorasickAutomata {
int ch[MAXNODE][SIGMA_SIZE];
int f[MAXNODE]; // fail函数
int val[MAXNODE]; // 每个字符串的结尾结点都有一个非0的val
int last[MAXNODE]; // 输出链表的下一个结点
int cnt[MAXS];
int sz; void init() {
sz = ;
memset(ch[], , sizeof(ch[]));
memset(cnt, , sizeof(cnt));
ms.clear();
} // 字符c的编号
int idx(char c) {
return c-'a';
} // 插入字符串 v必须非0
void insert(char *s, int v) {
int u = , n = strlen(s);
for(int i = ; i < n; i++) {
int c = idx(s[i]);
if(!ch[u][c]) {
memset(ch[sz], , sizeof(ch[sz]));
val[sz] = ;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] = v;
ms[string(s)] = v;
} // 递归打印以结点j结尾的所有字符串
void print(int j) {
if(j) {
cnt[val[j]]++;
print(last[j]);
}
} // 在T中找模板
int find(char* T) {
int n = strlen(T);
int j = ; // 当前结点编号 初始为根结点
for(int i = ; i < n; i++) { // 文本串当前指针
int c = idx(T[i]);
while(j && !ch[j][c]) j = f[j]; // 顺着细边走 直到可以匹配
j = ch[j][c];
if(val[j]) print(j);
else if(last[j]) print(last[j]); // 找到了
}
} // 计算fail函数
void getFail() {
queue<int> q;
f[] = ;
// 初始化队列
for(int c = ; c < SIGMA_SIZE; c++) {
int u = ch[][c];
if(u) { f[u] = ; q.push(u); last[u] = ; }
}
// 按BFS顺序计算fail
while(!q.empty()) {
int r = q.front(); q.pop();
for(int c = ; c < SIGMA_SIZE; c++) {
int u = ch[r][c];
if(!u) continue;
q.push(u);
int v = f[r];
while(v && !ch[v][c]) v = f[v];
f[u] = ch[v][c];
last[u] = val[f[u]] ? f[u] : last[f[u]];
}
}
} }; AhoCorasickAutomata ac;
char text[], P[][];
int n, T; int main() {
while(scanf("%d", &n) == && n) {
ac.init();
for(int i = ; i <= n; i++) {
scanf("%s", P[i]);
ac.insert(P[i], i);
}
ac.getFail();
scanf("%s", text);
ac.find(text);
int best = -;
for(int i = ; i <= n; i++)
if(ac.cnt[i] > best) best = ac.cnt[i];
printf("%d\n", best);
for(int i = ; i <= n; i++)
if(ac.cnt[ms[string(P[i])]] == best) printf("%s\n", P[i]);
}
return ;
}
Code 2:作者代码
时间:42 ms
由此可见:
因为只需要返回字串而与序号无关,即使前一个模板会被后一个相同模板覆盖,但不添加map标记处理相重是可以的,因为val插入时被修改所以被覆盖的单词不会被处理cnt==0 , 而最后的一个相同的串会被操作得到正确值,因此统计时依然可以返回正确值。
而且即使添加了map时间也不过是提高了4ms,因此并非作者在书中所言“容易忽略”而“多此一举”。
可是如果出现重复模板特别多的输入的话 预判是否相同进而选择添加是可以的,但作者的map处理好像也不能加速这种情况。
【暑假】[实用数据结构]UVAlive 4670 Dominating Patterns的更多相关文章
- UVALive 4670 Dominating Patterns --AC自动机第一题
题意:多个模板串,一个文本串,求出那些模板串在文本串中出现次数最多. 解法:AC自动机入门模板题. 代码: #include <iostream> #include <cstdio& ...
- uvalive 4670 Dominating Patterns
在文本串中找出现次数最多的子串. 思路:AC自动机模板+修改一下print函数. #include<stdio.h> #include<math.h> #include< ...
- UVALive - 4670 Dominating Patterns AC 自动机
input n 1<=n<=150 word1 word2 ... wordn 1<=len(wirdi)<=70 s 1<=len(s)<=1000000 out ...
- UVALive 4670 Dominating Patterns (AC自动机)
AC自动机的裸题.学了kmp和Trie以后不难看懂. 有一些变化,比如0的定义和f的指向,和建立失配边,以及多了后缀连接数组last.没有试过把失配边直接当成普通边(一开始还是先这样写吧). #inc ...
- 【暑假】[实用数据结构]UVAlive 3135 Argus
UVAlive 3135 Argus Argus Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %l ...
- 【暑假】[实用数据结构]UVAlive 3026 Period
UVAlive 3026 Period 题目: Period Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld ...
- 【暑假】[实用数据结构]UVAlive 3942 Remember the Word
UVAlive 3942 Remember the Word 题目: Remember the Word Time Limit: 3000MS Memory Limit: Unknown ...
- 【暑假】[实用数据结构]UVAlive 4329 Ping pong
UVAlive 4329 Ping pong 题目: Ping pong Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: % ...
- 【暑假】[实用数据结构]UVAlive 3027 Corporative Network
UVAlive 3027 Corporative Network 题目: Corporative Network Time Limit: 3000MS Memory Limit: 30000K ...
随机推荐
- wamp设置实现本机IP或者局域网访问 (转)
<Directory /> Options FollowSymLinks AllowOverride None Order deny,allow Allow from all #以前是De ...
- 10 things you should know about NoSQL databases
For a quarter of a century, the relational database (RDBMS) has been the dominant model for database ...
- struts2总结五: Strut2 访问Servlet API
一.间接访问 public String execute() throws Exception { this.message="hello,this is put into applicat ...
- 李洪强iOS开发之图片拉伸技巧
纵观移动市场,一款移动app,要想长期在移动市场立足,最起码要包含以下几个要素:实用的功能.极强的用户体验.华丽简洁的外观.华丽外观的背后,少不了美工的辛苦设计,但如果开发人员不懂得怎么合理展示这些设 ...
- gdb调试多线程程序总结
阿里核心系统团队博客 http://csrd.aliapp.com/?tag=pstack Linux下多线程查看工具(pstree.ps.pstack) http://www.cnblogs.com ...
- 将 Qt 5.6 集成至 VS2015
摘要: 由于VS2015不再支持addin,所以要用其他手段. 这里给出64位系统下的安装步骤,32位类似. 一.安装VS2015 过程略.值得注意的是要选择需要安装的内容,既然要用Qt,那么C++相 ...
- Delphi函数翻译成VC要注意句柄指针传递(传递Handle的时候,必须加上一个指针引用,才能消除编译错误)
Delphi里做了魔法变化,每个变量名称本身就是指针,因为不怎么需要指针语法.我也不知道是不是因为这个原因引起的Delphi与VC对句柄的不同处理. 这是Delphi的强行关机函数,好用,调用方式:W ...
- Vim插件列表
01.helm(Vim-Swoop) 02.ap/vim-buftabline 03.wesleyche/SrcExpl 04.vim proc 05.vim shell 06.dhruvasagar ...
- linux中应用程序main函数中没有开辟进程的,它应该在那个进程中运行呢?
1.main函数是一个进程还是一个线程? 不知道你是用c创建的,还是用java创建的. 因为它们都是以main()做为入口开始运行的. 是一个线程,同时还是一个进程. 在现在的操作系统中,都是多线程的 ...
- 【HDOJ】4652 Dice
1. 题目描述对于m面的骰子.有两种查询,查询0表示求最后n次摇骰子点数相同的期望:查询1表示最后n次摇骰子点数均不相同的期望. 2. 基本思路由期望DP推导,求得最终表达式.(1) 查询0 不 ...