uva 1401 Remember the Word(Trie dp)
2014-08-29 21:09
507 查看
s0s1...sn-1 是目标字符串
f[i] s0...si 的分解方案数
f[i + len - 1] = f[i + len - 1] + f[i - 1]
边界:f[-1] = 0
在Trie上dp
f[i] s0...si 的分解方案数
f[i + len - 1] = f[i + len - 1] + f[i - 1]
边界:f[-1] = 0
在Trie上dp
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <vector> #include <queue> #include <stack> #include <cassert> #include <algorithm> #include <cmath> #include <limits> #include <set> #include <time.h> using namespace std; #define MIN(a, b) a < b ? a : b #define MAX(a, b) a > b ? a : b #define F(i, n) for (int i=0;i<(n);++i) #define REP(i, s, t) for(int i=s;i<=t;++i) #define UREP(i, s, t) for(int i=s;i>=t;--i) #define REPOK(i, s, t, o) for(int i=s;i<=t && o;++i) #define MEM0(addr, size) memset(addr, 0, size) #define LBIT(x) x&-x #define PI 3.1415926535897932384626433832795 #define HALF_PI 1.5707963267948966192313216916398 #define eps 1e-15 #define MAXN 300000 #define MAXM 100 #define MOD 20071027 typedef long long LL; const double maxdouble = numeric_limits<double>::max(); int f[MAXN + 1]; int getval(int pos) { if (pos < 0) return 1; return f[pos]; } const int CHARSET = 26, BASE = 'a', MAX_NODE = 4000*MAXM; struct Trie { int child[MAX_NODE + 1][CHARSET]; int val[MAX_NODE + 1]; //bool isWord[MAX_NODE + 1]; int sz; Trie() { init(); } void init() { sz = 1; memset(child[0], 0, sizeof(child[0])); } int idx(char c) { return c - BASE; } void insert(const char *str) { int u = 0; int len = strlen(str); int c; for (int i=0;i<len;++i) { c = idx(str[i]); if (!child[u][c]) { memset(child[sz], 0, sizeof(child[sz])); val[sz] = 0; child[u][c] = sz++; } u = child[u][c]; } val[u] = 1; //isWord[u] = true; } int query(const char *str, int offset) { int u = 0; int len = strlen(str); int c; F(i, len) { c = idx(str[i]); if (!child[u][c]) return 0; u = child[u][c]; if (val[u]) { //cout << str + i << endl; f[offset + i] = (getval(offset - 1) + f[offset + i]) % MOD; } } return val[u]; } }; struct Trie trie; int main() { freopen("input.in", "r", stdin); //freopen("input.in", "w", stdout); int n, m; int len; int cnt; char str[MAXN + 5]; char buf[MAXM + 5]; cnt = 0; while (scanf("%s", str) != EOF) { if (strlen(str) == 0) continue; cin >> n; F(i, n) { scanf("%s", buf); trie.insert(buf); } len = strlen(str); F(i, len) trie.query(str + i, i); ++cnt; printf("Case %d: %d\n", cnt, f[len - 1]); trie.init(); memset(f, 0, sizeof(f)); } return 0; }
相关文章推荐
- UVA 1401 Remember the Word (trie + dp)
- UVa 1401 - Remember the Word(Trie + DP)
- UVA 1401 - Remember the Word(Trie+DP)
- UVA - 1401 Remember the Word(trie+dp)
- UVA 1401 - Remember the Word(Trie+DP)
- UVa 1401 Remember the Word(Trie+DP)
- uva 1401 Remember the Word ( Trie + DP )
- LA 3942 && UVa 1401 Remember the Word (Trie + DP)
- UVALive - 3942 Remember the Word[Trie DP]
- UVA 1401 Remember the Word(Trie树 套 DP)
- UVA 1401 Remember the Word(DP+字典树Trie)
- Uva 1401 - Remember the Word//TRie
- UVALive 3942 Remember the Word(trie + dp)
- uva1401 - Remember the Word 前缀树DP
- uva 1401 - Remember the Word(字典树+dp)
- UVALive - 3942 Remember the Word(trie + dp)
- UVA 1401 Remember the Word(DP+字典树Trie)
- UVA-1401-Remember the Word(Trie)
- UVA-1401 - Remember the Word -----Trie前缀树
- UVA 1401 Remember the Word(用Trie加速动态规划)