Lost's revenge
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 4638 Accepted Submission(s): 1301
Problem Description
Lost and AekdyCoin are friends. They always play "number game"(A boring game based on number theory) together. We all know that AekdyCoin is the man called "nuclear weapon of FZU,descendant of Jingrun", because of his talent in the field of number theory. So Lost had never won the game. He was so ashamed and angry, but he didn't know how to improve his level of number theory.
One noon, when Lost was lying on the bed, the Spring Brother poster on the wall(Lost is a believer of Spring Brother) said hello to him! Spring Brother said, "I'm Spring Brother, and I saw AekdyCoin shames you again and again. I can't bear my believers were being bullied. Now, I give you a chance to rearrange your gene sequences to defeat AekdyCoin!".
It's soooo crazy and unbelievable to rearrange the gene sequences, but Lost has no choice. He knows some genes called "number theory gene" will affect one "level of number theory". And two of the same kind of gene in different position in the gene sequences will affect two "level of number theory", even though they overlap each other. There is nothing but revenge in his mind. So he needs you help to calculate the most "level of number theory" after rearrangement.
One noon, when Lost was lying on the bed, the Spring Brother poster on the wall(Lost is a believer of Spring Brother) said hello to him! Spring Brother said, "I'm Spring Brother, and I saw AekdyCoin shames you again and again. I can't bear my believers were being bullied. Now, I give you a chance to rearrange your gene sequences to defeat AekdyCoin!".
It's soooo crazy and unbelievable to rearrange the gene sequences, but Lost has no choice. He knows some genes called "number theory gene" will affect one "level of number theory". And two of the same kind of gene in different position in the gene sequences will affect two "level of number theory", even though they overlap each other. There is nothing but revenge in his mind. So he needs you help to calculate the most "level of number theory" after rearrangement.
Input
There are less than 30 testcases.
For each testcase, first line is number of "number theory gene" N(1<=N<=50). N=0 denotes the end of the input file.
Next N lines means the "number theory gene", and the length of every "number theory gene" is no more than 10.
The last line is Lost's gene sequences, its length is also less or equal 40.
All genes and gene sequences are only contains capital letter ACGT.
For each testcase, first line is number of "number theory gene" N(1<=N<=50). N=0 denotes the end of the input file.
Next N lines means the "number theory gene", and the length of every "number theory gene" is no more than 10.
The last line is Lost's gene sequences, its length is also less or equal 40.
All genes and gene sequences are only contains capital letter ACGT.
Output
For each testcase, output the case number(start with 1) and the most "level of number theory" with format like the sample output.
Sample Input
3 AC CG GT CGAT 1 AA AAA 0
Sample Output
Case 1: 3 Case 2: 2
Author
Qinz@XDU
Source
Recommend
lcy
题意:给定n个模式串和一个目标串,求将目标串重排列后所能包含的最多的模式串个数
解题思路:可以开个dp[i][a][b][c][d]来记录状态转移,i为结点状态,但是a,b,c,d的值范围为[0,40],很明显数组爆了。假设ACGT的总数分别为num[0],num[1],num[2],num[3] 那么对于ACGT的数量分别为ABCD的状态可以记录为: A*(num[1]+1)*(num[2]+1)*(num[3]+1) + B*(num[2]+1)*(num[3]+1)+ C*(num[3]+1) +D 这样的状态最大就是11*11*11*11
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cctype>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
int n, cnt[4], bit[4];
char ch[100];
int dp[505][11 * 11 * 11 * 11 + 5];
struct Trie
{
int next[600][4], fail[600], flag[600];
int root, tot;
int newnode()
{
for (int i = 0; i < 4; i++) next[tot][i] = -1;
flag[tot++] = 0;
return tot - 1;
}
void init()
{
tot = 0;
root = newnode();
}
int get(char ch)
{
if (ch == 'A') return 0;
if (ch == 'C') return 1;
if (ch == 'G') return 2;
else return 3;
}
void insert(char ch[])
{
int k = root;
for (int i = 0; ch[i]; i++)
{
if (next[k][get(ch[i])] == -1) next[k][get(ch[i])] = newnode();
k = next[k][get(ch[i])];
}
flag[k]++;
}
void build()
{
queue<int>q;
fail[root] = root;
for (int i = 0; i < 4; 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 pre = q.front();
q.pop();
if (flag[fail[pre]]) flag[pre] += flag[fail[pre]];
for (int i = 0; i < 4; i++)
{
if (next[pre][i] == -1) next[pre][i] = next[fail[pre]][i];
else
{
fail[next[pre][i]] = next[fail[pre]][i];
q.push(next[pre][i]);
}
}
}
}
int solve(char ch[])
{
memset(cnt, 0, sizeof cnt);
for (int i = 0; ch[i]; i++) cnt[get(ch[i])]++;
bit[0] = (cnt[1] + 1)*(cnt[2] + 1)*(cnt[3] + 1);
bit[1] = (cnt[2] + 1)*(cnt[3] + 1);
bit[2] = cnt[3] + 1;
bit[3] = 1;
memset(dp, -1, sizeof dp);
dp[0][0] = 0;
for (int A = 0; A <= cnt[0]; A++)
for (int B = 0; B <= cnt[1]; B++)
for (int C = 0; C <= cnt[2]; C++)
for (int D = 0; D <= cnt[3]; D++)
{
int temp = A*bit[0] + B*bit[1] + C*bit[2] + D*bit[3];
for (int i = 0; i < tot; i++)
if (dp[i][temp] >= 0)
{
for (int j = 0; j < 4; j++)
{
if (j == 0 && cnt[j] == A) continue;
if (j == 1 && cnt[j] == B) continue;
if (j == 2 && cnt[j] == C) continue;
if (j == 3 && cnt[j] == D) continue;
dp[next[i][j]][temp + bit[j]] = max(dp[next[i][j]][temp + bit[j]], dp[i][temp] + flag[next[i][j]]);
}
}
}
int ans = 0, temp = cnt[0] * bit[0] + cnt[1] * bit[1] + cnt[2] * bit[2] + cnt[3] * bit[3];
for (int i = 0; i < tot; i++) ans = max(ans, dp[i][temp]);
return ans;
}
void debug()
{
for (int i = 0; i < tot; i++)
{
printf("id = %3d,fail = %3d,end = %3d,chi = [", i, fail[i], flag[i]);
for (int j = 0; j < 26; j++) printf("%2d", next[i][j]);
printf("]\n");
}
}
}ac;
int main()
{
int cas = 0;
while (~scanf("%d", &n) && n)
{
ac.init();
for (int i = 1; i <= n; i++)
{
scanf("%s", &ch);
ac.insert(ch);
}
ac.build();
scanf("%s", ch);
printf("Case %d: %d\n", ++cas, ac.solve(ch));
}
return 0;
}