本文主要是介绍Censored! POJ - 1625 ac自动机+dp+高精度,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、内容
The alphabet of Freeland consists of exactly N letters. Each sentence of Freeland language (also known as Freish) consists of exactly M letters without word breaks. So, there exist exactly N^M different Freish sentences.But after recent election of Mr. Grass Jr. as Freeland president some words offending him were declared unprintable and all sentences containing at least one of them were forbidden. The sentence S contains a word W if W is a substring of S i.e. exists such k >= 1 that S[k] = W[1], S[k+1] = W[2], ...,S[k+len(W)-1] = W[len(W)], where k+len(W)-1 <= M and len(W) denotes length of W. Everyone who uses a forbidden sentence is to be put to jail for 10 years.Find out how many different sentences can be used now by freelanders without risk to be put to jail for using it.
Input
The first line of the input file contains three integer numbers: N -- the number of letters in Freish alphabet, M -- the length of all Freish sentences and P -- the number of forbidden words (1 <= N <= 50, 1 <= M <= 50, 0 <= P <= 10).The second line contains exactly N different characters -- the letters of the Freish alphabet (all with ASCII code greater than 32).The following P lines contain forbidden words, each not longer than min(M, 10) characters, all containing only letters of Freish alphabet.
Output
Output the only integer number -- the number of different sentences freelanders can safely use.
Sample Input
2 3 1
ab
bb
Sample Output
5
二、思路
- 由于方案数很多 故应该使用高精度。 还有注意输入的字符超出了char的范围会变成负数。
三、代码
#include <cstdio>
#include <cstring>
#include <queue>
#include <map>
#include <vector>
using namespace std;
const int N = 55, M = 110;
struct BigInteger{int A[25];enum{MOD = 10000};BigInteger(){memset(A, 0, sizeof(A)); A[0]=1;}void set(int x){memset(A, 0, sizeof(A)); A[0]=1; A[1]=x;}void print(){printf("%d", A[A[0]]);for (int i=A[0]-1; i>0; i--){if (A[i]==0){printf("0000"); continue;}for (int k=10; k*A[i]<MOD; k*=10) printf("0");printf("%d", A[i]);}printf("\n");}int& operator [] (int p) {return A[p];}const int& operator [] (int p) const {return A[p];}BigInteger operator + (const BigInteger& B){BigInteger C;C[0]=max(A[0], B[0]);for (int i=1; i<=C[0]; i++)C[i]+=A[i]+B[i], C[i+1]+=C[i]/MOD, C[i]%=MOD;if (C[C[0]+1] > 0) C[0]++;return C;}BigInteger operator * (const BigInteger& B){BigInteger C;C[0]=A[0]+B[0];for (int i=1; i<=A[0]; i++)for (int j=1; j<=B[0]; j++){C[i+j-1]+=A[i]*B[j], C[i+j]+=C[i+j-1]/MOD, C[i+j-1]%=MOD;}if (C[C[0]] == 0) C[0]--;return C;}
};
int n, m, P, len, tr[M][N], ne[M], fail[M];
BigInteger dp[N][M];
char s[1000];
int mp[1000];
void add() {int p = 0;for (int i = 0; s[i]; i++) {int j = mp[(int)s[i] + 200];if (!tr[p][j]) tr[p][j] = ++len;p = tr[p][j];}fail[p] = 1;
}
void build() {queue<int> q;for (int j = 0; j < n; j++) {if (tr[0][j]) q.push(tr[0][j]);}while (!q.empty()) {int p = q.front(); q.pop();for (int j = 0; j < n; j++) {int c = tr[p][j];if (!c) tr[p][j] = tr[ne[p]][j];else {ne[c] = tr[ne[p]][j];fail[c] |= fail[ne[c]];q.push(c);} }}
} int main() {scanf("%d%d%d", &n, &m, &P);scanf("%s", s);for (int i = 0; i < n; i++) {mp[(int)s[i] + 200] = i;} for (int i = 1; i <= P; i++) {scanf("%s", s); add();}build();//进行dp求方案数 ]dp[0][0].set(1);for (int i = 1; i <= m; i++) {for (int j = 0; j <= len; j++) {for (int k = 0; k < n; k++) {int tj = tr[j][k]; //(i - 1, j)-->(i, tj)下一状态跳到哪儿 if (!fail[tj]) dp[i][tj] = dp[i][tj] + dp[i - 1][j];}} }BigInteger ans;for (int j = 0; j <= len; j++) ans = ans + dp[m][j];ans.print();return 0;
}
这篇关于Censored! POJ - 1625 ac自动机+dp+高精度的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!