【暑假】[实用数据结构]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 ...
随机推荐
- SSH开发框架搭建参考
一, 参考文章: 1, http://blog.csdn.net/communicate_/article/details/8644040 这篇文章讲的还算详尽,但是貌似有一些多余的代码: 2,
- Fiddler 日志
Fiddler 日志(Logging) 在开发扩展插件及编写FiddlerScript时对调试程序非常有用. 1.输出日志 在FiddlerScript脚本中,你可以这样输出输出日志: Fiddler ...
- Maven中的一点疑惑的地方
我们建立一个Maven项目,其结构如下:
- Struts中的 saveToken的方法
Struts中的 saveToken的方法 saveToken防止2次提交的问题 struts有自带的Token(令牌)的机制来解决重复提交(包括后退,刷新等).举例: 假设:假设有一个新增用 ...
- Java基于Servlet过虑器
- 使用jenkins自动部署java工程到jboss-eap6.3 -- 1.环境搭建
使用jenkins自动部署java工程到jboss-eap6.3 -- 1.环境搭建 目录 使用jenkins自动部署java工程到jboss-eap6.3 -- 1.环境搭建 使用jenkins自动 ...
- Android 常用UI控件之TabHost(5)Tab栏在底部且在最上层也不盖tab页
tab栏在底部 <TabHost android:id="@android:id/tabhost" android:layout_width="match_pare ...
- PHP操作FTP类 (上传下载移动创建等)
使用PHP操作FTP-用法 Php代码 收藏代码 <? // 联接FTP服务器 $conn = ftp_connect(ftp.server.com); // 使用username和passwo ...
- js设置datagriad的行移动
// ,// formatter: function(value,row,index){// ...
- Android 内存管理(二)
很多开发者都是从j2me或j2ee上过来的,对于内存的使用和理解并不是很到位,Android开发网本次给大家一些架构上的指导,防止出现豆腐渣工 程的出现.Android作为以Java语言为主的智能平台 ...