【暑假】[实用数据结构]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 ...
随机推荐
- mysql 权限管理
参考: http://www.cnblogs.com/Richardzhu/p/3318595.html 一.MySQL权限简介 关于mysql的权限简单的理解就是mysql允许你做你全力以内 ...
- checkbox复选框样式
随着现代浏览器的流行,纯CSS设置checkbox也变的很是实用,下面会讲到5种与众不同的checkbox复选框. 首先,需要添加一段CSS隐藏所有的Checkbox复选框,下面我们会改变它的外观.要 ...
- 【斜率DP】BZOJ 1010:玩具装箱
1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 7537 Solved: 2888[Submit][St ...
- [转载]js javascript 判断字符串是否包含某字符串,String对象中查找子字符,indexOf
var Cts = "bblText"; if(Cts.indexOf("Text") > 0 ) { alert('Cts中包含Text字符串'); }
- [itint5]环形最大连续子段和
http://www.itint5.com/oj/#9 一开始有了个n*n的算法,就是把原来的数组*2,由环形的展开成数组.然后调用n次最大子段和的方法.超时. 后来看到个O(n)的算法,就是如果不跨 ...
- POJ2503——Babelfish(map映射+string字符串)
Babelfish DescriptionYou have just moved from Waterloo to a big city. The people here speak an incom ...
- 编码识别工具:hash-identifier
hash-identifier的使用: 当不知道编码是什么类型的时候,可以通过kali系统中的hash工具判别,如下图所示, 在HASH后面输入要判别的编码内容,在后面的Possible Hashs中 ...
- SVN使用方法总结
SVN使用方法 SVN版本管理模式:http://www.cnblogs.com/newstar/archive/2011/01/04/svn.html (集中式-trunk和分散式-branch ...
- 关于xml的一些知识,DTD,XSD
DTD 文档类型定义(Document Type Definition)是一套关于标记符的语法规则.它是标准通用标记语言和 可扩展标记语言1.0版规格的一部分,是文档的验证机制.文档类型定义是一种保证 ...
- C语言字符串函数
strtok() 字符串分割函数strstr() 字符串查找函数 范例 #include <string.h> main() { char * s = " ...