NBUT1222-English Game

  • English Game

  • 时间限制: 1000 ms 内存限制: 131072 K
  • 问题描述
  • 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.

  • 输入
  • 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.
  • 输出
  • For each test case, output the biggest sum weight, if you could not form the string X, output -1.
  • 样例输入
  • 1 aaaa
    a 2
    3 aaa
    a 2
    aa 5
    aaa 6
    4 abc
    a 1
    bc 2
    ab 4
    c 1
    3 abcd
    ab 10
    bc 20
    cd 30
    3 abcd
    cd 100
    abc 1000
    bcd 10000
  • 样例输出
  • 8
    7
    5
    40
    -1
  • 提示
  • 来源
  • 辽宁省赛2010

解题思路:dp+字典树,将给出的n个字符串建字典树,然后进行dp,一开始dp数组全清为0,dp[0]为1,若dp[i-1]不为0,且从i到j可以找到一个字符串,字符串权值为w,则dp[j]=max(dp[i-1]+w,dp[j])


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <functional>
#include <climits>

using namespace std;

#define LL long long
const int INF=0x3f3f3f3f;

int n,m,f[100090][26],g[100090],dp[100090],cnt;
char s[100090],a[1000090];

int main()
{
    while(~scanf("%d",&n))
    {
        scanf("%s",s+1);
        int len=strlen(s+1);
        memset(f[0],0,sizeof f[0]);
        cnt=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%s%d",a,&m);
            int k=0;
            for(int j=0;j<strlen(a);j++)
            {
                if(!f[k][a[j]-'a'])
                {
                    f[k][a[j]-'a']=++cnt;
                    memset(f[cnt],0,sizeof f[cnt]);
                    g[cnt]=0;
                }
                k=f[k][a[j]-'a'];
            }
            g[k]=max(g[k],m);
        }
        memset(dp,0,sizeof dp);
        dp[0]=1;
        for(int i=1;i<=len;i++)
        {
            int k=0;
            if(!dp[i-1]) continue;
            for(int j=i;j<=i+30&&j<=len;j++)
            {
                if(f[k][s[j]-'a'])
                {
                    k=f[k][s[j]-'a'];
                    if(g[k]) dp[j]=max(dp[j],dp[i-1]+g[k]);
                }
                else break;
            }
        }
        printf("%d\n",dp[len]-1);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值