AC自动机+记忆化搜索uva1399Puzzle


Jisung is the student representative of the Department of Computer Engineering in ACM University. A few days later, the annual festival will be held for the students in the department. He is preparing some events for the festival. Since Jisung likes to make and solve puzzles, he decides to devise some interesting puzzle for the festival.

The followings are the rules for the puzzle made by Jisung:

(1)
The players will be given an integer n. Then they should use the first n capital letters from the Roman alphabet. For example, if n = 4, the four characters A, B, C, and D will be used to solve this puzzle.
(2)
The players will be given s forbidden strings. No forbidden string contains another forbidden string as a substring. The winner is the student who makes the longest string that does not include a forbidden string as a substring.
(3)
If such a longest string does not exist, i.e., if we can make arbitrarily long strings that satisfy the above condition, or if we cannot make any string that satisfies the above condition, ` No' should be answered.

For example, suppose the given number n = 2, i.e., the players can use the two characters A and B. Assume that the forbidden strings are {AAA, AB, BA, BB}. In this case, the longest string that does not include any of the four forbidden strings as substrings is AA. But if the given forbidden strings are {AAA, BBB, ABAB, BBAA}, we cannot make such a longest string since arbitrarily long concatenations of ABA, i.e., ABAABAABA ... do not include any forbidden string.

Input 

Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case starts with a line containing two integers n (1$ \le$n$ \le$26) and s (1$ \le$s$ \le$1, 000) which represent the number of characters and the number of forbidden strings, respectively. From the second line to (s + 1)-st line of the test case, the forbidden strings are given one by one. The length of a forbidden string does not exceed 50.

Output 

Your program is to write to standard output. Print exactly one line for each test case. Print the longest string that does not include any forbidden string as a substring if it exists, otherwise, just print `No' as output. When there exists more than one such longest string with the same length, print the lexicographically largest string among them.

Sample Input 

3 
2 4 
AAA 
AB 
BA 
BB 
2 4 
AAA 
BBB 
ABAB 
BBAA 
3 7 
AA 
ABA 
BAC 
BB 
BC 
CA 
CC

Sample Output 

AA 
No 
ACBAB
题意:给你一些字符串,让你构造一个最长的串,不能包含给出的串

思路:先建立AC自动机,然后判环,判环的时候要注意,用两个访问数组,然后根据AC自动机的状态进行转移

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=1000;
const int maxm=maxn*55;
const int SIGMA_SIZE=26;
int N,m,cnt,num;
int dp[maxm];
bool vis[maxm],vis1[maxm];
char s[60];
int zh[maxm][2];
struct AC
{
    int ch[maxm][26],val[maxm];
    int fail[maxm];
    int sz;
    void clear(){memset(ch[0],0,sizeof(ch[0]));sz=1;}
    int idx(char x){return x-'A';}
    void insert(char *s)
    {
        int n=strlen(s);
        int u=0;
        for(int i=0;i<n;i++)
        {
            int c=idx(s[i]);
            if(!ch[u][c])
            {
                memset(ch[sz],0,sizeof(ch[sz]));
                val[sz]=0;
                ch[u][c]=sz++;
            }
            u=ch[u][c];
        }
        val[u]=1;
    }
    void getfail()
    {
        queue<int> q;
        fail[0]=0;
        int u=0;
        for(int i=0;i<N;i++)
        {
            u=ch[0][i];
            if(u){fail[u]=0;q.push(u);}
        }
        while(!q.empty())
        {
            int r=q.front();q.pop();
            if(val[fail[r]])val[r]=1;
            for(int c=0;c<N;c++)
            {
                u=ch[r][c];
                if(!u){ch[r][c]=ch[fail[r]][c];continue;}
                q.push(u);
                int v=fail[r];
                while(v&&!ch[v][c]){v=fail[v];}
                fail[u]=ch[v][c];
            }
        }

    }
    bool find(int u)
    {
        vis1[u]=1;
        for(int i=0;i<N;i++)
        {
            int v=ch[u][i];
            if(vis[v])return true;
            if(val[v]==0&&vis1[v]==0)
            {
                vis[v]=1;
                if(find(v))return true;
                vis[v]=0;
            }
        }
        return false;
    }
    int dfs(int u)
    {
        if(vis[u])return dp[u];
        vis[u]=1;
        dp[u]=0;
        for(int c=N-1;c>=0;c--)
        {
            int v=ch[u][c];
            if(val[v])continue;
            int tmp=dfs(v)+1;
            if(dp[u]<tmp)
            {
                dp[u]=tmp;
                zh[u][0]=v;
                zh[u][1]=c;
            }
        }
        return dp[u];
    }
    void print(int u)
    {
        if(zh[u][0]==-1)return;
        printf("%c",zh[u][1]+'A');
        print(zh[u][0]);
    }
    bool solve()
    {
        memset(vis,0,sizeof(vis));
        memset(vis1,0,sizeof(vis1));
        vis[0]=1;
        if(find(0))return false;
        memset(vis,0,sizeof(vis));
        memset(zh,-1,sizeof(zh));
        if(dfs(0)==0)return false;
        print(0);
        printf("\n");
        return true;
    }
}ac;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&N,&m);
        ac.clear();
        for(int i=0;i<m;i++)
        {
            scanf("%s",s);
            ac.insert(s);
        }
        ac.getfail();
        if(!ac.solve())printf("No\n");
    }
    return 0;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值