ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

BZOJ3075,LG3082 [USACO13MAR]项链Necklace

2019-01-26 09:03:07  阅读:345  来源: 互联网

标签:BZOJ3075 ch her int AC LG3082 rg Necklace 自动机


题意

Bessie the cow has arranged a string of N rocks, each containing a single letter of the alphabet, that she wants to build into a fashionable necklace.

Being protective of her belongings, Bessie does not want to share her necklace with the other cow currently living on her side of the barn. The other cow has a name that is a string of M characters, and Bessie wants to be sure that this length-M string does not occur as a contiguous substring anywhere within the string representing her necklace (otherwise, the other cow might mistakenly think the necklace is for her). Bessie decides to remove some of the rocks in her necklace so that the other cow's name does not appear as a substring. Please help Bessie determine the minimum number of rocks she must remove.

贝西收集了N颗石头,每颗石头上都有一个字母,贝西想把这些石头做成项链。

贝西的身边有另一只奶牛,这只奶牛的名字是一个长度为M的字符串,贝西不希望这只牛的名字出现在她的项链上(项链的子串),她想知道,最少删掉几颗石头就可以避免这种情况发生。

分析

参照The_Virtuoso的题解。

首先如果用AC自动机做这道题显然要把B串建在AC自动机上(AC自动机上就一个串好像有点浪费qwq)。要想B串不出现在A串中,只要把A串在AC自动机上跑,使它一直不遍历到B串的终止节点就能保证B串不是A串的子串。想要最优解自然要dp,那么就可以定义f[i][j]表示A串的第i个字符匹配到了AC自动机上第j个节点保留的最长长度。对于A串上的每一个字符可以删除或者在AC自动机上往下走,最后用A串总长len减掉max{f[len][i]}就是最小删除数了。

时间复杂度\(O(N M)\)

代码

#include<bits/stdc++.h>
#define rg register
#define il inline
#define co const
template<class T>il T read()
{
    rg T data=0;
    rg int w=1;
    rg char ch=getchar();
    while(!isdigit(ch))
    {
        if(ch=='-')
            w=-1;
        ch=getchar();
    }
    while(isdigit(ch))
    {
        data=data*10+ch-'0';
        ch=getchar();
    }
    return data*w;
}
template<class T>il T read(rg T&x)
{
    return x=read<T>();
}
typedef long long ll;
using std::cerr;
using std::endl;

co int N=1e4+1,M=1e3+1;
char s[N],t[M];
namespace AC
{
    int tot;
    int ch[M][26],val[M];
    int fail[M];
    
    void ins(char s[],int n)
    {
        int u=0;
        for(int i=0;i<n;++i)
        {
            int k=s[i]-'a';
            if(!ch[u][k])
                ch[u][k]=++tot;
            u=ch[u][k];
        }
        val[u]=1;
    }
    
    void getfail()
    {
        std::queue<int>Q;
        for(int i=0;i<26;++i)
            if(ch[0][i])
                Q.push(ch[0][i]);
        while(Q.size())
        {
            int u=Q.front();Q.pop();
            for(int i=0;i<26;++i)
            {
                if(ch[u][i])
                {
                    fail[ch[u][i]]=ch[fail[u]][i];
                    Q.push(ch[u][i]);
                }
                else
                    ch[u][i]=ch[fail[u]][i];
            }
        }
    }
    
    int f[N][M];
    
    void solve(char s[],int n)
    {
        for(int i=0;i<n;++i)
        {
            int k=s[i]-'a';
            for(int j=0;j<=tot;++j)
            {
                if(!val[ch[j][k]])
                    f[i+1][ch[j][k]]=std::max(f[i+1][ch[j][k]],f[i][j]+1);
                if(!val[j])
                    f[i+1][j]=std::max(f[i+1][j],f[i][j]);
            }
        }
        int ans=0;
        for(int i=0;i<=tot;++i)
            ans=std::max(ans,f[n][i]);
        printf("%d\n",n-ans);
    }
}

int main()
{
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    scanf("%s",s);
    scanf("%s",t);
    AC::ins(t,strlen(t));
    AC::getfail();
    AC::solve(s,strlen(s));
    return 0;
}

标签:BZOJ3075,ch,her,int,AC,LG3082,rg,Necklace,自动机
来源: https://www.cnblogs.com/autoint/p/10322512.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有