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 ...
随机推荐
- “玲珑杯”ACM比赛 Round #23
A -- 生是脂肪的人 Time Limit:2s Memory Limit:128MByte Submissions:263Solved:97 DESCRIPTION 给定一个整数n,输出[(10^ ...
- iOS学习笔记35-社交分享
一.社交分享 现在很多应用都内置“社交分享”功能,可以将看到的新闻.博客.广告等内容分享到微博.微信.QQ.人人网等,其实从iOS6.0开始苹果官方就内置了Social.framework专门来实现社 ...
- BZOJ 4870 [Shoi2017]组合数问题 ——动态规划 矩阵乘法
注意到$r<k$ 别问我为什么要强调. 考场上前30分水水. 然后写阶乘的时候大力$n\log {n}$预处理 本机跑的挺快的,然后稳稳的T掉了. 然后就是简单的矩阵乘法了. #include ...
- 【CCF】无线网络 搜索+思维
#include<iostream> #include<cstdio> #include<cstring> #include<string> #incl ...
- python 序列化之pickle模块 json模块
一 pickle import pickle s='dd' print(pickle.dumps(s)) 输出: b'\x80\x03X\x02\x00\x00\x00ddq\x00.' pickle ...
- 事务的传播行为和隔离级别[transaction behavior and isolated level]
Spring中事务的定义:一.Propagation : key属性确定代理应该给哪个方法增加事务行为.这样的属性最重要的部份是传播行为.有以下选项可供使用: PROPAGATION_REQUIRED ...
- 洛谷 P 1133 教主的花园
题目描述 教主有着一个环形的花园,他想在花园周围均匀地种上n棵树,但是教主花园的土壤很特别,每个位置适合种的树都不一样,一些树可能会因为不适合这个位置的土壤而损失观赏价值. 教主最喜欢3种树,这3种树 ...
- 【CF1025C】Plasticine zebra(模拟)
题意: n<=1e5 思路:可以证明答案即为极长交替出现的串长度之和 需要注意其实这个串是一个环,复制后再做 #include<cstdio> #include<cstring ...
- HDU 6218 (线段树+set)
HDU 6218 Bridge Problem : 给一个2×n的矩阵,一开始矩阵所有相邻点之间有一条边.有其.个询问,每次给出两个相邻的点的坐标,将其中的边删除或者添加,问如此操作之后整张图的割边数 ...
- 用户空间使用i2c_dev
============================================作者:yuanluluhttp://blog.csdn.NET/yuanlulu 版权没有,但是转载请保留此段声 ...