UVaLive/LA 6806 Hari Merdeka(AC自动机,DP)
2014-11-23 21:26
357 查看
// Author: Yuan Zhu #include <iostream> #include <cstdio> #include <cstring> #include <queue> #define maxn 20010 #define ll long long using namespace std; int t; int N, M, B; int v[30], c[210]; int next[maxn][26], fail[maxn]; int L, rt, end[maxn]; char C[30]; inline int newnode() { memset(next[L], 0, sizeof next[L]); end[L++] = 0; return L - 1; } inline void init() { L = 0; rt = newnode(); } inline void insert(char *s, int z) { int l = strlen(s), x = rt; for (int i = 0; i < l; i++) { int z = s[i] - 'A'; if(!next[x][z]) next[x][z] = newnode(); x = next[x][z]; } end[x] = z; } inline void build() { queue<int> q; fail[0] = 0; for (int i = 0; i < 26; i++) { if (next[rt][i] != 0) { fail[next[rt][i]] = rt; q.push(next[rt][i]); } } while (!q.empty()) { int x = q.front(); q.pop(); for (int i = 0; i < 26; i++) { if (next[x][i] == 0) { next[x][i] = next[fail[x]][i]; } else { fail[next[x][i]] = next[fail[x]][i]; q.push(next[x][i]); } } } } void read() { scanf("%d%d%d", &N, &M, &B); int value; for (int i = 0; i < N; i++) { scanf("\n%c %d", &c[i], &value); v[i] = value; } char s[110]; for (int i = 0; i < M; i++) { scanf("%s%d", s, &value); insert(s, value); } } ll dp[210][maxn], cost[maxn]; void work(int ca) { memset(dp, -1, sizeof dp); dp[0][0] = 0; ll ans = 0; for (int i = 1; i < L; i++) { cost[i] = 0; int x = i; while (x) { cost[i] += end[x]; x = fail[x]; } //cout<<i<<" "<<cost[i]<<endl; } for (int i = 0; i <= B; i++) { for (int j = 0; j < L; j++) { if (dp[i][j] == -1) continue; for (int k = 0; k < N; k++) { if (i + v[k] > B) continue; int nxt = next[j][c[k] - 'A']; dp[i + v[k]][nxt] = max(dp[i + v[k]][nxt], dp[i][j] + cost[nxt]); //cout<<i+v[k]<<" "<<nxt<<" "<<dp[i+v[k]][nxt]<<endl; } } } for (int i = 0; i <= B; i++) { for (int j = 0; j < L; j++) { ans = max(ans, dp[i][j]); } } printf("Case #%d: %lld\n", ca, ans); } int main() { scanf("%d", &t); for (int ca = 1; ca <= t; ca++) { init(); read(); build(); work(ca); } return 0; }
相关文章推荐
- UVALive 3907 (LA 3907) Puzzle AC自动机 + 搜索DP 记录路径
- UVALive 4126 Password Suspects(AC自动机 套 DP)
- UVALive 4126 Password Suspects(AC自动机 + DP)
- UVaLive/LA 6801 Sequence(DP)
- UVaLive LA 4256 | UVa 1424 - Salesmen(简单DP)
- UVALive 3026 Period (KMP上的dp,学习ac自动机的前奏)
- UVALive 4126 (LA 4126) Password Suspects AC自动机 + DP + 剪枝dfs
- UVALive - 4256 || LA 4256 Salesmen 商人(DP)
- UVaLive 4126 - Password Suspects (AC自动机 DP)
- UVaLive ( LA ) 3516 - Exploring Pyramids (DP 递推)
- UVaLive 3490 - Generator (AC自动机 期望DP 高斯消元)
- UVaLive/LA 6800 The Mountain of Gold?(BellmanFord判负环+记忆化搜索)
- UVALive 3490 (LA 3940) || ZOJ 2619 Generator AC自动机(或KMP) + 整数高斯消元 + 数学期望
- UVALive - 3942 Remember the Word(字典树+dp)
- UVALIVE 3346 Perfect Domination on Trees 树形DP
- UVALive 3703 (LA 3703) Billing Tables Trie树
- UVALive 4764 简单dp水题(也可以暴力求解)
- UVALIVE 3516(DP)
- UVALive 4256 Salesmen 【简单dp】
- uvalive 6669 hidden tree(好壮压dp)