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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

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
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
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
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;
}

你可能感兴趣的:(NOIP解题报告,kmp,杂)