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;
}
时间: 2024-08-06 11:51:33

Hnu 11187 Emoticons :-) (ac自己主动机+贪心)的相关文章

字符串算法之 AC自己主动机

近期一直在学习字符串之类的算法,感觉BF算法,尽管非常easy理解,可是easy超时,全部就想学习其它的一些字符串算法来提高一下,近期学习了一下AC自己主动机.尽管感觉有所收获,可是还是有些朦胧的感觉,在此总结一下,希望大家不吝赐教. 一.AC自己主动机的原理: Aho-Corasick automaton.该算法在1975年产生于贝尔实验室,是著名的多模匹配算法之中的一个. 一个常见的样例就是给出N个单词,在给出一段包括m个字符的文章,让你找出有多少个单词在这文章中出现过,.要搞懂AC自己主动

数据结构与算法系列----AC自己主动机

一:概念 首先简要介绍一下AC自己主动机:Aho-Corasick automation,该算法在1975年产生于贝尔实验室,是著名的多模匹配算法之中的一个.一个常见的样例就是给出n个单词,再给出一段文章(长度是m),让你找出有多少个单词在文章里出现过. 要搞懂AC自己主动机.先得有字典树Trie的基础知识(也有人说需要KMP的知识,我认为暂且不要理会这个. 可是在看这篇文章之前,Trie字典树,你是必需要先搞懂,假设你还不理解Trie,请參考http://blog.csdn.net/laoji

【UVA】11468-Substring(AC自己主动机)

AC自己主动机的题,须要注意的,建立失配边的时候,假设结点1失配边连到的那个结点2,那个结点2是一个单词的结尾,那么这个结点1也须要标记成1(由于能够看成,这个结点包括了这个单词),之后在Trie树上进行行走,每次走到下一个能够走的结点. 14378527 11468 Substring Accepted C++ 0.585 2014-10-19 10:35:00 #include<cstdio> #include<cstring> #include<algorithm>

hdoj 2222 Keywords Search 【AC自己主动机 入门题】 【求目标串中出现了几个模式串】

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 44687    Accepted Submission(s): 14103 Problem Description In the modern time, Search engine came into the life of everybody li

【UVA】1449-Dominating Patterns(AC自己主动机)

AC自己主动机的模板题.须要注意的是,对于每一个字符串,须要利用map将它映射到一个结点上,这样才干按顺序输出结果. 14360841 1449 option=com_onlinejudge&Itemid=8&page=show_problem&problem=4195" style="font-size:13.3333330154419px; margin:0px; padding:0px; color:rgb(153,0,0); text-decoratio

HDU - 3341 Lost&amp;#39;s revenge(AC自己主动机+DP)

Description Lost and AekdyCoin are friends. They always play "number game"(A boring game based on number theory) together. We all know that AekdyCoin is the man called "nuclear weapon of FZU,descendant of Jingrun", because of his talen

HDU 2222 Keywords Search(AC自己主动机模板题)

题意:给出一个字符串和若干个模板,求出在文本串中出现的模板个数. 思路:由于有可能有反复的模板,trie树权值记录每一个模板出现的次数就可以. #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #include<vector> #include<map&

ZOJ - 3228 Searching the String (AC自己主动机)

Description Little jay really hates to deal with string. But moondy likes it very much, and she's so mischievous that she often gives jay some dull problems related to string. And one day, moondy gave jay another problem, poor jay finally broke out a

hdu4758 Walk Through Squares (AC自己主动机+DP)

Walk Through Squares Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 944 Accepted Submission(s): 277 Problem Description On the beaming day of 60th anniversary of NJUST, as a military college whic