HUNAN 11187 Emoticons :-) (ac自动机+贪心)

题意:

给出n个表情串,现在给出一篇txt,要求将txt对应的字母改成空格使得txt不包含如何表情串。求最小操作数。

题解:

dp肯定挂,无论是内存或者时间的消耗都挂。

那么就要思考下能否用贪心做。

根据大犇,我也学着证明一下,对于这样的文章,如果出现表情串,那么有一下几种情况:

1、各个表情串单独的分开,这个好说,直接碰到匹配ans++;

2、如果两个或多个重叠,比如a,b,c重叠

(1)a,b,c互相重叠,那么很显然,a是重叠最多的部分,那么只要将a的结尾修改就好。

(2)a,b,重叠,c,b也重叠,但是a,c不重叠,这种情况明显首先修改a的结尾,那么b的部分就已经不是表情串了,不用修改!那么现在这种情况就变成了1的情况,多个串分开,因为b已经不是表情串了,所以c是单独的。

3、包含关系,这个也好说,肯定是选择被包含的串的结尾进行修改。

以上是贪心的证明。所以每次只要匹配了就马上返回根部。

不知道为什么总是RE,好奇怪。


#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
#define B(x) (1<<(x))
//typedef __int64 ll;
//typedef unsigned __int64 Ull;
const int oo=0x3f3f3f3f;
//const ll OO=1LL<<61;
//const Ull MOD=1000000;
const int maxn=30;
const int SIZE=3003;
const int type=256;
char str[maxn];

struct ACautomaton
{
    int next[SIZE][type],fail[SIZE],word[SIZE];
    int cnt,root;

    int newNode()
    {
        for(int i=0;i<type;i++)
            next[cnt][i]=-1;
        word[cnt++]=0;
        return cnt-1;
    }

    void Init()
    {
        cnt=0;
        root=newNode();
    }

    void Insert(char buff[])
    {
        int now=root;
        for(int i=0,k;buff[i];i++)
        {
            k=buff[i];
            if(next[now][k]==-1)
                next[now][k]=newNode();
            now=next[now][k];
        }
        word[now]=1;
    }

    void build()
    {
        fail[root]=root;
        int now=root;
        queue<int>Q;
        for(int i=0;i<type;i++)
        {
            if(next[now][i]==-1)
                next[now][i]=root;
            else
            {
                fail[next[now][i]]=root;
                Q.push(next[now][i]);
            }
        }
        while(!Q.empty())
        {
            now=Q.front();
            Q.pop();
            if(word[fail[now]]) word[now]=1;
            for(int i=0;i<type;i++)
            {
                if(next[now][i]==-1)
                    next[now][i]=next[fail[now]][i];
                else
                {
                    fail[next[now][i]]=next[fail[now]][i];
                    Q.push(next[now][i]);
                }
            }
        }
    }

    int greed()
    {
        int ans=0;
        int now=root,temp;
        char ch;
        while((ch=getchar())!='\n')
        {
            if(ch==' ')
            {
                now=root;
                continue;
            }
            now=next[now][ch];
            temp=now;
            while(temp!=root)
            {
                if(temp==-1)break;
                if(word[temp])
                {
                    ans++;
                    now=root;
                    break;
                }
                temp=fail[temp];
            }
        }
        return ans;
    }

};
ACautomaton ac;

int main()
{
    int n,m,ans;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        if(n==0&&m==0)break;
        ac.Init();
        for(int i=1;i<=n;i++)
        {
            scanf("%s",str);
            ac.Insert(str);
        }
        ac.build();
        ans=0;
        for(int i=1;i<=m;i++)
        {
            getchar();
            ans+=ac.greed();///贪心,边读入边判断,用贪心思想处理
        }
        printf("%d\n",ans);
    }
    return 0;
}
/**
*/





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值