SPOJ 7758. Growing Strings (ac自动机+dp)

题目大意:

给出了N个串。问最多有多少个串组成的序列,是可以由上一个串通过左右两边加字符构成的。

思路分析:

在trie上的dp

在建立自动机的时候,得到fail的同时,用dp记录这个串作为最后一个串所可以得到的最多的满足要求的串的数量。

那么 dp[i] = max(dp[i在trie上的的父亲节点],dp[i的fail节点] )+ 以i节点结尾的单词的数量,注意不是以i字符结尾。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#define maxn 1000005
using namespace std;
const char tab = 'a';
const int max_next = 26;

int next[maxn][max_next],fail[maxn],num[maxn],dp[maxn],siz;
int newnode()
{
    for(int i=0;i<max_next;i++)
        next[siz][i]=0;
    fail[siz]=num[siz]=dp[siz]=0;
    return siz++;
}
void Insert(char *s,int len)
{
    int p=0;
    for(int i=0;i<len;i++)
    {
        int &x=next[p][s[i]-tab];
        p=x?x:x=newnode();
    }
    num[p]++;
}

int ans=0;
void acbuild()
{
    queue<int>Q;
    Q.push(0);
    while(!Q.empty())
    {
        int temp=Q.front();
        Q.pop();
        for(int i=0;i<max_next;i++)
        {
            int v=next[temp][i];
            if(v==0)next[temp][i]=next[fail[temp]][i];
            else Q.push(v);
            if(temp!=0)fail[v]=next[fail[temp]][i];

            dp[v]=max(dp[temp],dp[fail[v]])+num[v];
            ans=max(ans,dp[v]);
        }
    }
}
int query(char *s,int len)
{
    int cnt=0;
    for(int i=0,p=0;i<len;i++)
    {
        p=next[p][s[i]-tab];
        for(int f=p;f;f=fail[f]){
            if(num[f]>0)cnt++;
        }
    }
    return cnt;
}
char word[maxn];

int main()
{
    int N;
    while(scanf("%d",&N)!=EOF && N)
    {
        ans=0;
        siz=0;
        newnode();

        for(int i=1;i<=N;i++)
        {
            scanf("%s",word);
            Insert(word,strlen(word));
        }

        acbuild();
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-08-30 01:09:03

SPOJ 7758. Growing Strings (ac自动机+dp)的相关文章

UVALive - 4811 Growing Strings (AC自动机+dp)

题目链接:UVALive - 4811 Growing Strings 题意: 给你n个字符串,问你最多能选出多少个字符串,使得s[i]是s[i+1]的子串. 题解: 先将所有的字符串插入AC自动机,将所有字符串按长度排序后,显然dp[i]=max{dp[j]}+1,其中s[j]是s[i]的子串.然后就完了. 1 #include<bits/stdc++.h> 2 #define mst(a,b) memset(a,b,sizeof(a)) 3 #define F(i,a,b) for(int

SPOJ 7758 Growing Strings

MGLAR10 - Growing Strings Gene and Gina have a particular kind of farm. Instead of growing animals and vegetables, as it is usually the case in regular farms, they grow strings. A string is a sequence of characters. Strings have the particularity tha

hdu 2457 AC自动机+dp

DNA repair Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2004    Accepted Submission(s): 1085 Problem Description Biologists finally invent techniques of repairing DNA that contains segments c

HDU3341 Lost&#39;s revenge(AC自动机+DP)

题目是给一个DNA重新排列使其包含最多的数论基因. 考虑到内存大概就只能这么表示状态: dp[i][A][C][G][T],表示包含各碱基个数为ACGT且当前后缀状态为自动机第i的结点的字符串最多的数论基因数 其中ACGT可以hash成一个整数(a*C*G*T+c*G*T+g*T+T),这样用二维数组就行了,而第二维最多也就11*11*11*11个. 接下来转移依然是我为人人型,我是丢进一个队列,用队列来更新状态的值. 这题果然挺卡常数的,只好手写队列,最后4500msAC,还是差点超时,代码也

poj 1625 Censored!(AC自动机+DP+高精度)

题目链接:poj 1625 Censored! 题目大意:给定N,M,K,然后给定一个N字符的字符集和,现在要用这些字符组成一个长度为M的字符串,要求不包 括K个子字符串. 解题思路:AC自动机+DP+高精度.这题恶心的要死,给定的不能匹配字符串里面有负数的字符情况,也算是涨姿势 了,对应每个字符固定偏移128单位. #include <cstdio> #include <cstring> #include <queue> #include <vector>

HDU 2296 Ring AC自动机 + DP

题意:给你n个模式串,每个模式串有一个得分,让你构造出一个长度为N之内且分数最高的文本串;输出字典序列最小的. 解题思路:  AC自动机 + DP , 不过要输出字典序列最小,多开一个 一个三维字符串来辅助二维DP(新思路) , DP[i][j] ,表示到i位置状态为j的最大得分. 解题代码: 1 // File Name: temp.cpp 2 // Author: darkdream 3 // Created Time: 2014年09月11日 星期四 15时18分4秒 4 5 #inclu

HDU2296——Ring(AC自动机+DP)

题意:输入N代表字符串长度,输入M代表喜欢的词语的个数,接下来是M个词语,然后是M个词语每个的价值.求字符串的最大价值.每个单词的价值就是单价*出现次数.单词可以重叠.如果不止一个答案,选择字典序最小的. 题解:AC自动机+dp.dp[i][j]表示在字符串长度i,在自动机的第j个状态.因为要字典序最小,所以转移时要保持字典序最小. 想了各种转移姿势 最后还是查了题解 发现可以直接记录前缀转移…… #include <bits/stdc++.h> using namespace std; co

hdu 2296 aC自动机+dp(得到价值最大的字符串)

Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3180    Accepted Submission(s): 1033 Problem Description For the hope of a forever love, Steven is planning to send a ring to Jane with a rom

POJ1625 Censored!(AC自动机+DP)

题目问长度m不包含一些不文明单词的字符串有多少个. 依然是水水的AC自动机+DP..做完后发现居然和POJ2778是一道题,回过头来看都水水的... dp[i][j]表示长度i(在自动机转移i步)且后缀状态为自动机第j个结点的合法字符串数 dp[0][0]=1 转移转移... 注意要用高精度,因为答案最多5050. 还有就是要用unsigned char,题目的输入居然有拓展的ASCII码,编码128-255. 1 #include<cstdio> 2 #include<cstring&