NEU1007: 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.


//我们很容易得出这样的动态规划方程式 
//对所有的的 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;
}

还是得好好学习ing。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值