const int MAXLEN = 11000; char ipt[MAXN][50], goal[MAXLEN]; int val[MAXN], len[MAXN], ans[MAXN]; int dp[MAXLEN]; const int maxm = 31000; struct Trie { int numptr; struct Node { Node* son[26]; int ptr; void init() { CLR(son, (int)NULL); ptr = -1; } } s[maxm], root; void init() { numptr = 0; root.init(); } void ins(char *ch, int z) { Node* ptr = &root; for(int i = 0; ch[i] != 0; i++) { int x = ch[i] - 'a'; if(ptr->son[x] == NULL) { s[numptr].init(); ptr->son[x] = &s[numptr++]; } ptr = ptr->son[x]; } ptr->ptr = z; } int find(char *ch) { int cnt = 0; Node *ptr = &root; for(int i = 0; ch[i] != 0; i++) { if (ptr->ptr != -1) ans[cnt++] = ptr->ptr; int x = ch[i] - 'a'; if(ptr->son[x] == NULL) { return cnt; } ptr = ptr->son[x]; } if (ptr->ptr != -1) ans[cnt++] = ptr->ptr; return cnt; } } trie; int main() { int n; while (~scanf("%d%s", &n, goal + 1)) { trie.init(); CLR(dp, -1); dp[0] = 0; FE(i, 1, n) { scanf("%s%d", ipt[i] + 1, &val[i]); len[i] = strlen(ipt[i] + 1); trie.ins(ipt[i] + 1, i); } int l = strlen(goal + 1); FE(i, 0, l - 1) { if (dp[i] == -1) continue; int cnt = trie.find(goal + i + 1); REP(j, cnt) { int ind = ans[j]; int nxt = len[ind] + i; if (dp[nxt] < dp[i] + val[ind]) dp[nxt] = dp[i] + val[ind]; } } WI(dp[l]); } return 0; }
原文地址:http://blog.csdn.net/wty__/article/details/38399483