【暑假】[实用数据结构]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 ...
随机推荐
- hdu 3449
有依赖的背包,转化成01背包来做: #include<iostream> #include<cstdio> #include<cstring> #include&l ...
- hdu 4403
水水的dfs #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath& ...
- Samza在YARN上的启动过程 =》 之二 submitApplication
首先,来看怎么构造一个org.apache.hadoop.yarn.client.api.YarnClient class ClientHelper(conf: Configuration) exte ...
- PHP 7 值得期待的新特性(下)
这是我们期待已久的 PHP 7 系列文章的第二篇.点此阅读 第一篇本文系 OneAPM 工程师编译整理. 也许你已经知道,重头戏 PHP 7 的发布将在今年到来!现在,让我们来了解一下,新版本有哪些新 ...
- 批量扫描互联网无线路由设备telnet,并获取WIFI密码
批量扫描互联网无线路由设备telnet,并获取WIFI密码 http://lcx.cc/?i=4513
- 【BZOJ 1038】 1038: [ZJOI2008]瞭望塔
1038: [ZJOI2008]瞭望塔 Description 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如下图所示 ...
- WebLoigc的配置(生产模式与开发模式)
1.Weblogic两种模式的切换1).生产模式--->开发模式将domain路径下的bin\setDomainEnv.cmd文件中set PRODUCTION_MODE=true改为set P ...
- 图形学:图像围绕着某个点P(a,b)旋转------白话版
前提:在研究图形时候,我们并没有规定图形的大小,所以任意图形多是支持的,这也另外说明了一点,图形转换和图形的大小没有关系. 如果图像围绕着某个点P(a,b)旋转,则先要将坐标系平移到该点,再进行旋转, ...
- C语言itoa()函数和atoi()函数详解(整数转字符)
http://c.biancheng.net/cpp/html/792.html C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. 以下是用itoa()函数将整 ...
- Fundamental Datastructure
11988 - Broken Keyboard (a.k.a. Beiju Text) 可以用deque来模拟. #include <iostream> #include <stri ...