HDU 6086 Rikka with String AC自动机 + DP

Rikka with String

 

Problem Description
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:

Yuta has n 01 strings si, and he wants to know the number of 01 antisymmetric strings of length 2L which contain all given strings si as continuous substrings.

01 string s is antisymmetric if and only if s[i]s[|s|i+1] for all i[1,|s|].

It is too difficult for Rikka. Can you help her?

In the second sample, the strings which satisfy all the restrictions are 000111,001011,011001,100110.
 
Input
The first line contains a number t(1t5), the number of the testcases. 

For each testcase, the first line contains two numbers n,L(1n6,1L100)

Then n lines follow, each line contains a 01 string si(1|si|20).
 
Output
For each testcase, print a single line with a single number -- the answer modulo 998244353.
 
Sample Input
2 2 2 011 001 2 3 011 001
 
Sample Output
1 4
 
 
题解:
  要是能在比赛中A掉就爽了
  和题解做法一样
  
#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N = 1e4+10, M = 1e3+20,inf = 2e9;

const LL mod = 998244353LL;

int dp[2][121][121][80],sum[2][N];
int nex[2][N][2],cnt0,cnt1,head1,tail1,head0,tail0,q[2][N],fail[2][N];

void insert(char *s,int p) {
    int now = 1,len = strlen(s);
    for(int i = 0; i < len; ++i) {
        int index = s[i] - '0';
        if(!nex[0][now][index])
            nex[0][now][index] = ++cnt0;
        sum[0][nex[0][now][index]] |= sum[0][now];
        now = nex[0][now][index];
        //cout<<now<<" "<<index<<endl;
    }
    sum[0][now] |= (1<<p);


    now = 1;
    for(int i = len-1; i >= 0; --i) {
        int index = s[i] - '0';
        if(!nex[1][now][index])
            nex[1][now][index] = ++cnt1;
        sum[1][nex[1][now][index]] |= sum[1][now];
        now = nex[1][now][index];
        //cout<<now<<" "<<index<<endl;
    }
    sum[1][now] |= (1<<p);
}

void build_fail() {
    head0 = 0, tail0 = 0;head1 = 0, tail1 = 0;
    for(int i = 0; i < 2; ++i)
        nex[0][0][i] = 1,nex[1][0][i] = 1;

    fail[0][1] = 0,fail[1][1] = 0;
    q[0][tail0++] = 1;q[1][tail1++] = 1;
    while(head0 != tail0) {
        int now = q[0][head0++];
        sum[0][now] |= sum[0][fail[0][now]];
        for(int i = 0; i < 2; ++i) {
            int p = fail[0][now];
            if(!nex[0][now][i]) {
                nex[0][now][i] = nex[0][p][i];continue;
            }
            fail[0][nex[0][now][i]] = nex[0][p][i];
            q[0][tail0++] = nex[0][now][i];
        }
    }
    while(head1 != tail1) {
        int now = q[1][head1++];
        sum[1][now] |= sum[1][fail[1][now]];
        for(int i = 0; i < 2; ++i) {
            int p = fail[1][now];
            if(!nex[1][now][i]) {
                nex[1][now][i] = nex[1][p][i];continue;
            }
            fail[1][nex[1][now][i]] = nex[1][p][i];
            q[1][tail1++] = nex[1][now][i];
        }
    }
}
int len[N],mx,n,L;
char a[N];
int dfs() {
    int now = 1;
    int ret = 0;
    for(int i = 1; i <= 2*mx; ++i) {
        now = nex[0][now][len[i]];
        ret |= sum[0][now];
    }
    return ret;
}
int ma(int p) {
    int now = 1;
    if(p)
        for(int i = mx; i >= 1; --i)
            now = nex[1][now][len[i]];
    else
        for(int i = mx+1; i <= 2*mx; ++i)
            now = nex[0][now][len[i]];
    return now;
}
void init() {
    memset(dp,0,sizeof(dp));
    memset(nex,0,sizeof(nex));
    cnt0 = 1;mx = -1;cnt1 = 1;
    memset(fail,0,sizeof(fail));
    memset(sum,0,sizeof(sum));
}
int main() {
    int T;
    scanf("%d",&T);
    while(T--) {
        scanf("%d%d",&n,&L);
        init();
        for(int i = 1; i <= n; ++i) {
            scanf("%s",a);
            insert(a,i-1);
            mx = max(mx,(int)strlen(a));
        }
        int ff = 0;
        mx-=1;
        build_fail();
        for(int i = 0; i < (1<<mx); ++i) {
            for(int j = 1; j <= mx; ++j) len[j] = ((i>>(j-1))&1);
            for(int j = mx+1; j <= 2*mx; ++j) len[j] = 1^(len[2*mx - j + 1]);
            int now = dfs();
            int z = ma(1),f = ma(0);
            dp[ff][z][f][now] += 1;
             dp[ff][z][f][now] %= mod;
           // cout<<i<<" "<<now<<" "<<z<<" "<<f<<endl;
        }

        for(int i = mx; i < L; i++) {
            memset(dp[ff^1],0,sizeof(dp[ff^1]));
            for(int j = 0; j < tail1; ++j) {
                for(int k = 0; k < tail0; ++k) {
                    for(int h = 0; h < (1<<n); ++h) {

                        if(!dp[ff][q[1][j]][q[0][k]][h]) continue;

                        int p = nex[1][q[1][j]][0],np = nex[0][q[0][k]][1];
                        int tmp = (h|sum[1][p]);
                        tmp |= sum[0][np];

                        dp[ff^1][p][np][tmp] += dp[ff][q[1][j]][q[0][k]][h];
                         dp[ff^1][p][np][tmp] %= mod;

                         p = nex[1][q[1][j]][1],np = nex[0][q[0][k]][0];
                        tmp = (h|sum[1][p]);
                        tmp |= sum[0][np];

                          dp[ff^1][p][np][tmp] += dp[ff][q[1][j]][q[0][k]][h];
                         dp[ff^1][p][np][tmp] %= mod;

                    }
                }
            }
            ff^=1;
        }
        LL ans = 0;
        for(int i = 0; i < tail1; ++i)
            for(int j = 0; j < tail0; ++j)
                ans = ( ans + dp[ff][q[1][i]][q[0][j]][(1<<n)-1]) % mod;
        printf("%lld\n",ans);
    }
    return 0;
}

 

 

posted @ 2017-08-08 20:25  meekyan  阅读(721)  评论(1编辑  收藏  举报