AC自动机专题——L - Wireless Password HDU - 2825 状压DP+AC自动机

Liyuan lives in a old apartment. One day, he suddenly found that there was a wireless network in the building. Liyuan did not know the password of the network, but he got some important information from his neighbor. He knew the password consists only of lowercase letters 'a'-'z', and he knew the length of the password. Furthermore, he got a magic word set, and his neighbor told him that the password included at least k words of the magic word set (the k words in the password possibly overlapping). 

For instance, say that you know that the password is 3 characters long, and the magic word set includes 'she' and 'he'. Then the possible password is only 'she'. 

Liyuan wants to know whether the information is enough to reduce the number of possible passwords. To answer this, please help him write a program that determines the number of possible passwords. 
Input
There will be several data sets. Each data set will begin with a line with three integers n m k. n is the length of the password (1<=n<=25), m is the number of the words in the magic word set(0<=m<=10), and the number k denotes that the password included at least k words of the magic set. This is followed by m lines, each containing a word of the magic set, each word consists of between 1 and 10 lowercase letters 'a'-'z'. End of input will be marked by a line with n=0 m=0 k=0, which should not be processed.
Output
For each test case, please output the number of possible passwords MOD 20090717.
Sample Input
10 2 2
hello 
world 
4 1 1
icpc 
10 0 0
0 0 0
Sample Output
2
1
14195065


题目大意:给定N个单词,求至少包含其中K个的长度为Len的字符串数量。

解题思路:利用状压DP暴力求解 ,最后统计一下其满足条件的字符串个数。

DP[len][i][m]  代表长度为len ,i 个节点,在状态为m下满足条件的个数


Status Accepted
Time 702ms
Memory 8188kB
Length 5234
Lang G++
Submitted
Shared
RemoteRunId 20411126
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(int i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define MOD 20090717
#define pb push_back
//#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef  vector<int> vi;
typedef  long long ll;
typedef  unsigned long long  ull;
typedef  unsigned int  ul;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}

ll qpow(ll p,ll q){
    ll f=1;
    while(q)
    {
        if(q&1) f=f*p%MOD;
        p=p*p%MOD;
        q>>=1;
    }
    return f;
}

int num[(1<<10)];


const int N = 1010;
const int M = 26;

class Trie
{

public:
    int next[N][M],fail[N],end[N];
    int root,L;
    int newnode()
    {
        for(int i = 0;i < M;i++)//每一个节点对应0-128中的任意一个。
            next[L][i] = -1;
        end[L++] = 0;//表示下面没有节点 初始化,如果是记录次数,就赋0 还可以赋任意的数,
        return L-1;
    }
    void init()
    {
        L = 0;
        memset(end,0,sizeof(end));
        root = newnode();
    }
    void insert(char s[],int id)
    {
        int len = strlen(s);
        int now = root;
        for(int i = 0;i < len;i++)
        {
            if(next[now][s[i]-'a'] == -1)
                next[now][s[i]-'a'] = newnode();
            now=next[now][s[i]-'a'];//记录其对应的节点编号
        }
        end[now] |= (1<<id);//初始化状压数据
        //end[now]++;也可以用匹配单词结束后来记录次数
    }
    void build()
    {
        queue<int>Q;
        fail[root] = root;//根节点仍然是根节点
        for(int i = 0;i < M;i++)//对第一个字符遍历
            if(next[root][i] == -1)//没有此字符开头
                next[root][i] = root;//跳转到根
            else//有此字符开头的
            {
                fail[next[root][i]] = root;//这个行位的失败指针为根
                Q.push(next[root][i]);//行放入队列
            }
        while(!Q.empty())//还有字符
        {
            int now = Q.front();//逐层拿出第一个
            Q.pop();
            end[now] |= end[fail[now]];//如果两个都能达到一个字符串,就就加上状压数据
            //if(end[fail[now]]== 1) end[now] = 1;
            for(int i = 0;i < M;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]);//下一行继续
                }
        }
    }


    /*bool used[N];//判断其是否被查找到
    bool query(char buf[],int n,int id)
    {
        int len = strlen(buf);
        int now = root;
        memset(used,false,sizeof(used));//初始化used
        bool flag = false;
        for(int i = 0;i < len;i++)
        {
            now = next[now][buf[i]];
            int temp = now;
            while(temp != root)
            {
                if(end[temp] != -1)
                {
                    used[end[temp]] = true;//记录被匹配的信息
                    flag = true;
                }
                temp = fail[temp];
            }
        }

    }*/
    int dp[110][N][1<<10];

    int solve(int n,int k1)
    {
        int len = n;
        for(int i=0;i<=len;i++)
        {
            for (int j = 0; j < L; ++j) {
                for (int k = 0; k < 1<<10; ++k) {
                    dp[i][j][k] = 0;
                }
            }
        }


        dp[0][root][0] = 1;
        for (int i = 0; i < len; ++i) {
            for (int j = 0; j < L; ++j) {//代表点
                for (int l = 0; l < 1<<10; ++l) {
                    if(dp[i][j][l]<=0) continue;
                    for (int k = 0; k < 26; ++k) {
                        int news = next[j][k];
                        int kk = l|end[news];//状压dp
                        dp[i+1][news][kk] += dp[i][j][l];
                        dp[i+1][news][kk] %= MOD;
                    }
                }
            }
        }



        int ans  = 0;
        for (int m = 0; m < 1<<10; ++m) {
            if (num[m] < k1) continue;
            for (int l = 0; l < L; ++l) {
                ans += dp[len][l][m];
                ans %=MOD;
            }
        }

        return ans;
    }
};

Trie ac;
char buf[110][N];


int n,m;

int main()
{
    for (int i = 0; i < (1<<10); i++) {
        num[i] = 0;
        for (int j = 0; j < 10; j++)
            if (i & (1<<j))
                num[i]++;
    }



    int T,t,k1;
    while(scanf("%d%d%d", &n,&m,&k1))
    {
        if(n==0&&m==0&&k1==0) break;
        ac.init();
        for(int i = 0; i < m; i++)
        {
            scanf("%s",buf[i]);
            ac.insert(buf[i],i);
        }
        ac.build();
        cout << ac.solve(n,k1)<<endl;
    }
    return 0;
}








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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值