Hnu 11187 Emoticons :-) (ac自己主动机+贪心)
题目大意:
破坏文本串。使之没有没有出现表情。破坏就是用空格替换。问最少须要破坏多少个字符。
思路分析:
初看跟Hdu 2457 没什么差别,事实上Hdu2457是要求将字符替换成ACGT,而这个仅仅须要替换成空格。
而空格是在表情串中不曾出现的。所以要破坏的时候就要遍历的指针赋为根节点,继续遍历。。
每一次变成根的时候ans就加一。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std; const char tab = 0;
const int max_next = 128;
int idx;
struct trie
{
struct trie *fail;
struct trie *next[max_next];
int isword;
int index;
trie()
{
isword=0;
index=0;
memset(next,NULL,sizeof next);
fail=NULL;
}
};
int rev[256];
trie *que[100005],ac[100005];
int head,tail; trie *New()
{
trie *temp=&ac[idx];
for(int i=0;i<max_next;i++)temp->next[i]=NULL;
temp->fail=NULL;
temp->isword=0;
temp->index=idx++;
return temp;
}
void Insert(trie *root,char *word,int len){
trie *t=root;
for(int i=0;i<len;i++){
if(t->next[word[i]]==NULL)
t->next[word[i]]=New();
t=t->next[word[i]];
}
t->isword++;
} void acbuild(trie *root){
int head=0,tail=0;
que[tail++]=root;
root->fail=NULL;
while(head<tail){
trie *temp=que[head++],*p;
for(int i=0;i<max_next;i++){
if(temp->next[i]){
if(temp==root)temp->next[i]->fail=root;
else {
p=temp->fail;
while(p!=NULL){
if(p->next[i]){
temp->next[i]->fail=p->next[i];
break;
}
p=p->fail;
}
if(p==NULL)temp->next[i]->fail=root;
}
if(temp->next[i]->fail->isword)temp->next[i]->isword++;
que[tail++]=temp->next[i];
}
else if(temp==root)temp->next[i]=root;
else temp->next[i]=temp->fail->next[i];
}
}
}
void del(trie *root)
{
for(int i=0;i<max_next;i++)
if(root->next[i])del(root->next[i]);
free(root);
} int dp[205][205];
int solve(char *word,int len,trie *root)
{
trie *r=root;
int ans=0;
for(int i=0;i<len;i++)
{
if(r->next[word[i]]->isword)r=root,ans++;
else r=r->next[word[i]];
}
return ans;
}
char word[10005];
int main()
{
int n,cas=1,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
getchar();
if(n==0 && m==0)break;
idx=0;
trie *root=New();
for(int i=1;i<=n;i++)
{
gets(word);
Insert(root,word,strlen(word));
}
acbuild(root);
int ans=0;
while(m--){
gets(word);
ans+=solve(word,strlen(word),root);
}
printf("%d\n",ans);
}
return 0;
}
Hnu 11187 Emoticons :-) (ac自己主动机+贪心)的更多相关文章
- POJ 2778 DNA Sequence (AC自己主动机 + dp)
DNA Sequence 题意:DNA的序列由ACTG四个字母组成,如今给定m个不可行的序列.问随机构成的长度为n的序列中.有多少种序列是可行的(仅仅要包括一个不可行序列便不可行).个数非常大.对10 ...
- hdu 2222 Keywords Search ac自己主动机
点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- 【UVA】1449-Dominating Patterns(AC自己主动机)
AC自己主动机的模板题.须要注意的是,对于每一个字符串,须要利用map将它映射到一个结点上,这样才干按顺序输出结果. 14360841 1449 option=com_onlinejudge& ...
- POJ 3691 & HDU 2457 DNA repair (AC自己主动机,DP)
http://poj.org/problem?id=3691 http://acm.hdu.edu.cn/showproblem.php?pid=2457 DNA repair Time Limit: ...
- HDU 2896 病毒侵袭 AC自己主动机题解
本题是在text里面查找key word的增强版.由于这里有多个text. 那么就不能够简单把Trie的叶子标志记录改动成-1进行加速了,能够使用其它技术.我直接使用个vis数组记录已经訪问过的节点, ...
- NYOJ 1085 数单词 (AC自己主动机模板题)
数单词 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描写叙述 为了可以顺利通过英语四六级考试,如今大家每天早上都会早起读英语. LYH本来以为自己在6月份的考试中能够通过六 ...
- hdu 4057 AC自己主动机+状态压缩dp
http://acm.hdu.edu.cn/showproblem.php?pid=4057 Problem Description Dr. X is a biologist, who likes r ...
- Keywords Search (ac 自己主动机)
Keywords Search Problem Description In the modern time, Search engine came into the life of everybod ...
- HDU - 2825 Wireless Password(AC自己主动机+DP)
Description Liyuan lives in a old apartment. One day, he suddenly found that there was a wireless ne ...
随机推荐
- 九度oj 题目1465:最简真分数
题目描述: 给出n个正整数,任取两个数分别作为分子和分母组成最简真分数,编程求共有几个这样的组合. 输入: 输入有多组,每组包含n(n<=600)和n个不同的整数,整数大于1且小于等于1000. ...
- 九度oj 题目1357:疯狂地Jobdu序列
题目描述: 阳仔作为OJ的数据管理员,每一周的题目录入都让其很抓狂,因为题目不是他出的,他控制不了出题的速度……在等题目的时候,阳仔又不敢出去打篮球,所以只能在纸上乱涂乱写,这天,阳仔在纸上写下了这样 ...
- js版本下拉菜单
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- PHP杂技(一)
逻辑运算符 &&和& ||和|的部分区别 返回结果类型不同, A||B 如果A为真那么B不会做判断,而A|B前后都做判断 switch判断中并不是===,更像是==,例如(1) ...
- Codeforces 899D Shovel Sale
题目大意 给定正整数 $n$($2\le n\le 10^9$). 考虑无序整数对 $(x, y)$($1\le x,y\le n, x\ne y$). 求满足 「$x+y$ 结尾连续的 9 最多」的 ...
- bzoj 2801 [Poi2012]Minimalist Security 设一个,求出所有
题目大意 给出一个N个顶点.M条边的无向图,边(u,v)有权值w(u,v),顶点i也有权值p(i), 并且对于每条边(u,v)都满足p(u)+p(v)>=w(u,v). 现在要将顶点i的权值减去 ...
- hdu 3992 AC自动机上的高斯消元求期望
Crazy Typewriter Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- net9:磁盘目录文件保存到XML文档及其XML文档的读写操作,以及绑定XML到treeview
原文发布时间为:2008-08-10 -- 来源于本人的百度文章 [由搬家工具导入] directorytoxml类: using System;using System.Data;using Sys ...
- yii模板中常用变量总结
yii模板中常用的一些变量总结. 现有这样一个url:http://www.phpernote.com/demos/helloworld/index.php/xxx/xxx 则通过如下方式获取的值对应 ...
- nVidia的物理系统
PhysX PhysX(wiki en 中文,physx wiki physx wiki2)是nVidia公司一款跨平台实时物理引擎,可使用硬件(GPU.PPU: Physics Process ...