English Game 字典樹排序 字符串處理

題目描述:

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.


  1. //我們很容易得出這樣的動態規劃方程式   
  2. //對所有的的 str[i] ,  
  3. len=strlen(str[i]);   
  4. for(j=0;j < strlen(source);j++)  
  5. if(strncmp(soucrStr+j-len+1,str,len)==0){  
  6.    fp[j] = max(fp[j],fp[j-len+1] + weight[i]);  
  7. }  
  8. //難點即是對字符串的處理,因爲數據量太大,普通的方法很容易超時。。我原先寫的是1103MS。。。那用KMP寫的。。過不過。。現在用字典樹排序處理後,94MS。  


附上代碼:

  1. /* 
  2.  * @user ipqhjjybj 
  3.  * @Time 94MS 
  4.  * @data 20130629 
  5.  */  
  6. #include <cstdio>  
  7. #include <cmath>  
  8. #include <cstdlib>  
  9. #include <ctime>  
  10.   
  11. #include <iostream>  
  12. #include <cmath>  
  13. #include <algorithm>  
  14. #include <numeric>  
  15. #include <utility>  
  16.   
  17. #include <cstring>  
  18. #include <vector>  
  19. #include <stack>  
  20. #include <queue>  
  21. #include <map>  
  22. #include <string>  
  23. using namespace std;  
  24.   
  25. #define inf 0x3f3f3f3f  
  26. #define MAXN 1010  
  27. #define MAXX 10010  
  28. #define clr(x,k) memset((x),(k),sizeof(x))  
  29. #define clrn(x,k) memset((x),(k),(n+1)*sizeof(int))  
  30. #define cpy(x,k) memcpy((x),(k),sizeof(x))  
  31. #define Base 10000  
  32.   
  33. typedef vector<int> vi;  
  34.   
  35. #define foreach(it,c) for(vi::iterator it = (c).begin();it != (c).end();++it)  
  36.   
  37. #define max(a,b) ((a)>(b)?(a):(b))  
  38. #define min(a,b) ((a)<(b)?(a):(b))  
  39.   
  40. struct node{  
  41.     int end; //表示一個字符串結束了 1表示結束,0表示未結束  
  42.     int w;   //  
  43.     struct node * son[26];  
  44. };  
  45. int n,nx;  
  46. int fp[MAXX]; //動態規劃存儲答案  
  47. struct node* root,bufChild[33*MAXN];  
  48. int bufCnt,len;  
  49. char s[MAXX],str[33];  
  50. void reverse(char *s){  
  51.     int i=0,j=strlen(s)-1,temp;  
  52.     while(i<j){  
  53.         temp=s[i];  
  54.         s[i]=s[j];  
  55.         s[j]=temp;  
  56.         i++,j--;  
  57.     }  
  58. }  
  59. node* newNode(){  
  60.     int i;  
  61.     node* cur = &bufChild[bufCnt++];  
  62.     cur->end=0;  
  63.     cur->w=0;  
  64.     for(i=0;i<26;i++)  
  65.         cur->son[i]=NULL;  
  66.     return cur;  
  67. }  
  68. void insert(const char *str,int weight){  
  69.     int len=strlen(str),i,j,x;  
  70.     node* temp = root;  
  71.     for(i = 0;i < len;i++){  
  72.         x = str[i]-'a';  
  73.         if(temp->son[x]==NULL){  
  74.             temp->son[x]=newNode();  
  75.         }  
  76.         temp=temp->son[x];  
  77.     }  
  78.     temp->end=1;  
  79.     temp->w=weight;  
  80. }  
  81. void Query(int Q){  
  82.     int j=Q;  
  83.     int x ;  
  84.     node *cur = root;  
  85.     while(s[j]){  
  86.         x = s[j]-'a';  
  87.         if(cur->son[x]!=NULL){  
  88.             if(cur->son[x]->end){  
  89.                 if(fp[j-1]!=-1)  
  90.                     fp[Q]=max(fp[Q],fp[j-1]+cur->son[x]->w);  
  91.             }  
  92.             cur = cur->son[x];  
  93.         }else{  
  94.             break;  
  95.         }  
  96.         j--;  
  97.     }  
  98. }  
  99. int main(){  
  100.     //freopen("1007.in","r",stdin);  
  101.     int i,w;  
  102.     s[0]=0;  
  103.     while(scanf("%d %s",&n,s+1)!=EOF){  
  104.         len = strlen(s+1);  
  105.         bufCnt=0;  
  106.         root=newNode();  
  107.         for(i = 0;i < n;i++){  
  108.             scanf("%s %d",str,&w);  
  109.             reverse(str);  //爲了方便的原因。逆序  
  110.             insert(str,w);  
  111.         }  
  112.         fp[0]=0;  
  113.         for(i = 1;i <= len;i++){  
  114.             fp[i]=-1;  
  115.             Query(i);  
  116.         }  
  117.         printf("%d\n",fp[len]);  
  118.     }  
  119.     return 0;  
  120. }  
還是得好好學習ing。。
發佈了28 篇原創文章 · 獲贊 8 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章