題目描述:
This English game is a simple English words connection game.
The rules are as follows: there are N English words in a dictionary, and every word has its own weight v. There is a weight if the corresponding word is used. Now there is a target string X. You have to pick some words in the dictionary, and then connect them to form X. At the same time, the sum weight of the words you picked must be the biggest.
Input:
There are several test cases. For each test, N (1<=n<=1000) and X (the length of x is not bigger than 10000) are given at first. Then N rows follow. Each row contains a word wi (the length is not bigger than 30) and the weight of it. Every word is composed of lowercases. No two words in the dictionary are the same.
Output:
For each test case, output the biggest sum weight, if you could not form the string X, output -1.
- //我們很容易得出這樣的動態規劃方程式
- //對所有的的 str[i] ,
- len=strlen(str[i]);
- for(j=0;j < strlen(source);j++)
- if(strncmp(soucrStr+j-len+1,str,len)==0){
- fp[j] = max(fp[j],fp[j-len+1] + weight[i]);
- }
- //難點即是對字符串的處理,因爲數據量太大,普通的方法很容易超時。。我原先寫的是1103MS。。。那用KMP寫的。。過不過。。現在用字典樹排序處理後,94MS。
附上代碼:
- /*
- * @user ipqhjjybj
- * @Time 94MS
- * @data 20130629
- */
- #include <cstdio>
- #include <cmath>
- #include <cstdlib>
- #include <ctime>
- #include <iostream>
- #include <cmath>
- #include <algorithm>
- #include <numeric>
- #include <utility>
- #include <cstring>
- #include <vector>
- #include <stack>
- #include <queue>
- #include <map>
- #include <string>
- using namespace std;
- #define inf 0x3f3f3f3f
- #define MAXN 1010
- #define MAXX 10010
- #define clr(x,k) memset((x),(k),sizeof(x))
- #define clrn(x,k) memset((x),(k),(n+1)*sizeof(int))
- #define cpy(x,k) memcpy((x),(k),sizeof(x))
- #define Base 10000
- typedef vector<int> vi;
- #define foreach(it,c) for(vi::iterator it = (c).begin();it != (c).end();++it)
- #define max(a,b) ((a)>(b)?(a):(b))
- #define min(a,b) ((a)<(b)?(a):(b))
- struct node{
- int end; //表示一個字符串結束了 1表示結束,0表示未結束
- int w; //
- struct node * son[26];
- };
- int n,nx;
- int fp[MAXX]; //動態規劃存儲答案
- struct node* root,bufChild[33*MAXN];
- int bufCnt,len;
- char s[MAXX],str[33];
- void reverse(char *s){
- int i=0,j=strlen(s)-1,temp;
- while(i<j){
- temp=s[i];
- s[i]=s[j];
- s[j]=temp;
- i++,j--;
- }
- }
- node* newNode(){
- int i;
- node* cur = &bufChild[bufCnt++];
- cur->end=0;
- cur->w=0;
- for(i=0;i<26;i++)
- cur->son[i]=NULL;
- return cur;
- }
- void insert(const char *str,int weight){
- int len=strlen(str),i,j,x;
- node* temp = root;
- for(i = 0;i < len;i++){
- x = str[i]-'a';
- if(temp->son[x]==NULL){
- temp->son[x]=newNode();
- }
- temp=temp->son[x];
- }
- temp->end=1;
- temp->w=weight;
- }
- void Query(int Q){
- int j=Q;
- int x ;
- node *cur = root;
- while(s[j]){
- x = s[j]-'a';
- if(cur->son[x]!=NULL){
- if(cur->son[x]->end){
- if(fp[j-1]!=-1)
- fp[Q]=max(fp[Q],fp[j-1]+cur->son[x]->w);
- }
- cur = cur->son[x];
- }else{
- break;
- }
- j--;
- }
- }
- int main(){
- //freopen("1007.in","r",stdin);
- int i,w;
- s[0]=0;
- while(scanf("%d %s",&n,s+1)!=EOF){
- len = strlen(s+1);
- bufCnt=0;
- root=newNode();
- for(i = 0;i < n;i++){
- scanf("%s %d",str,&w);
- reverse(str); //爲了方便的原因。逆序
- insert(str,w);
- }
- fp[0]=0;
- for(i = 1;i <= len;i++){
- fp[i]=-1;
- Query(i);
- }
- printf("%d\n",fp[len]);
- }
- return 0;
- }