zoukankan      html  css  js  c++  java
  • NBUT 1222 English Game 2010辽宁省赛

    Time limit 1000 ms

    Memory limit 131072 kB

    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.

    Sample Input

    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

    Sample Output

    8
    7
    5
    40
    -1

    用给的每个小字符串的价值拼出给的大字符串的最大价值

    我自己还不会,先记一下同校大牛的代码,再研究

     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<string>
     4 #include<string.h>
     5 #include<algorithm>
     6 #include<map>
     7 #include<cmath>
     8 using namespace std;
     9 const int N = 10005;
    10 struct node
    11 {
    12     int l,r,v;
    13     int nt;
    14 }p[1000000];
    15 int h[10005],cnt;
    16 void add(int l,int r,int v)
    17 {
    18     p[++cnt].l = l;
    19     p[cnt].r = r;
    20     p[cnt].v = v;
    21     p[cnt].nt = h[l];
    22     h[l]=cnt;
    23 }
    24 
    25 char s[10005];
    26 char c[1005][35];
    27 int dp[10005];
    28 int l[1005],v[1005],n,len;
    29 
    30 
    31 int next[50];
    32 int slen;
    33 
    34 void getNext(int x)
    35 {
    36     int j, k;
    37     j = 0; k = -1; next[0] = -1;
    38     while(j < l[x])
    39         if(k == -1 || c[x][j] == c[x][k])
    40             next[++j] = ++k;
    41         else
    42             k = next[k];
    43 }
    44 void KMP_Count(int x)
    45 {
    46     int ans = 0;
    47     int i, j = 0;
    48 
    49     if(slen == 1 && l[x] == 1)
    50     {
    51         if(s[0] == c[x][0])
    52            add(0,1,v[x]);
    53         else return;
    54     }
    55     getNext(x);
    56     for(i = 0; i < slen; i++)
    57     {
    58         while(j > 0 && s[i] != c[x][j])
    59             j = next[j];
    60         if(s[i] == c[x][j])
    61             j++;
    62         if(j == l[x])
    63         {
    64             add(i-l[x]+1,i+1,v[x]);
    65             j = next[j];
    66         }
    67     }
    68 }
    69 int main()
    70 {
    71     while(scanf("%d%s",&n,s)!=EOF)
    72     {
    73         cnt = 0;
    74         memset(h,0,sizeof(h));
    75         slen = len = strlen(s);
    76         memset(dp,-1,sizeof(dp));
    77         dp[0]=0;
    78         for(int i=1;i<=n;i++)
    79         {
    80             scanf("%s%d",c[i],&v[i]);
    81             l[i] = strlen(c[i]);
    82             KMP_Count(i);
    83         }
    84         for(int i=0;i<len;i++)
    85         {
    86             if(dp[i]==-1) continue;
    87             for(int j=h[i];j>0;j=p[j].nt)
    88             {
    89                 dp[p[j].r] = max(dp[p[j].r], dp[i]+p[j].v);
    90             }
    91         }
    92         printf("%d
    ",dp[len]);
    93     }
    94     return 0;
    95 }


  • 相关阅读:
    TTL与RS-485电平转换芯片MAX485/MAX3485
    RS485芯片介绍及典型应用电路
    脉冲电能表的组成及脉冲装置工作原理
    django-redis的安装及使用
    Python折线图——机器人UPtime Trend Chart
    ASP.Net DropDownList控件的使用方法
    C# ASP.Net数据库连接(Oracle)
    django根据已有数据库表生成model类
    Python Outlook发送邮件
    oracle将excel数据导入数据库
  • 原文地址:https://www.cnblogs.com/Annetree/p/6641675.html
Copyright © 2011-2022 走看看