NOI模拟(5.23) TJOID2T1 str (bzoj5337)

str

题目背景:

5.23 模拟 TJOI2018D2T1

分析:kmp + DP

 

我们定义f[i][j]表示枚举到第i个串,当前匹配到s的第j位的方案数,那么每一次只需要将第i段中的每一个可能串和s做匹配,然后将匹配上的位置转移一下就可以了,边界就是f[0][i]0 <= i <= len全部置为1就可以了,时间复杂度O(k*ai*|s|)

PS:原题没有标注取模,但是实际上有模1000000007

 

Source:

 

/*
	created by scarlyw
*/
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <set>
#include <queue>
#include <ctime>
#include <deque>
#include <iterator>
#include <map>

inline char read() {
	static const int IN_LEN = 1024 * 1024;
	static char buf[IN_LEN], *s, *t;
	if (s == t) {
		t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
		if (s == t) return -1;
	}
	return *s++;
}

/*
template<class T>
inline void R(T &x) {
	static char c;
	static bool iosig;
	for (c = read(), iosig = false; !isdigit(c); c = read()) {
		if (c == -1) return ;
		if (c == '-') iosig = true;	
	}
	for (x = 0; isdigit(c); c = read()) 
		x = ((x << 2) + x << 1) + (c ^ '0');
	if (iosig) x = -x;
}
//*/

const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;
inline void write_char(char c) {
	if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
	*oh++ = c;
}

template<class T>
inline void W(T x) {
	static int buf[30], cnt;
	if (x == 0) write_char('0');
	else {
		if (x < 0) write_char('-'), x = -x;
		for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
		while (cnt) write_char(buf[cnt--]);
	}
}

inline void flush() {
	fwrite(obuf, 1, oh - obuf, stdout);
}

// /*
template<class T>
inline void R(T &x) {
	static char c;
	static bool iosig;
	for (c = getchar(), iosig = false; !isdigit(c); c = getchar()) {
		if (c == -1) return ;
		if (c == '-') iosig = true;	
	}
	for (x = 0; isdigit(c); c = getchar()) 
		x = ((x << 2) + x << 1) + (c ^ '0');
	if (iosig) x = -x;
}
// */

const int MAXS = 10000 + 10;
const int MAXN = 100 + 5;
const int mod = 1000000000 + 7;

int n, k, sl, len;
int f[MAXN][MAXS], p[MAXS];
char s[MAXS], t[MAXS];

inline void add(int &x, int t) {
    (x += t), ((x >= mod) ? (x -= mod) : x);
}

inline void get_fail(char *s, int len) {
    int j = 0;
    p[1] = 0;
    for (int i = 2; i <= len; ++i) {
        while (j != 0 && s[j + 1] != s[i]) j = p[j];
        if (s[j + 1] == s[i]) ++j;
        p[i] = j;
    }
}

inline void solve() {
    R(n), scanf("%s", s + 1), len = strlen(s + 1);
    for (int i = 0; i <= len; ++i) f[0][i] = 1;
    for (int c = 1; c <= n; ++c) {
        R(k);
        while (k--) {
            scanf("%s", t + 1), sl = strlen(t + 1), get_fail(t, sl);
            int j = 0;
            for (int i = 1; i <= len; ++i) {
                while (j != 0 && t[j + 1] != s[i]) j = p[j];
                if (t[j + 1] == s[i]) {
                    ++j;
                    if (j == sl) add(f[c][i], f[c - 1][i - sl]), j = p[j];
                }
            }
        }
    }
    int ans = 0;
    for (int i = 1; i <= len; ++i) add(ans, f[n][i]);
    std::cout << ans; 
}

int main() {
    freopen("str.in", "r", stdin);
    freopen("str.out", "w", stdout);
    solve();
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值