zoukankan      html  css  js  c++  java
  • NEFU English Game 字符串 dp 字典树

    English Game

    Time Limit 1000ms

    Memory Limit 65536K

    description

      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
    							

    ----------------------

    f[i]=max(f[j]+cost) 字符串j+1...i存在

    用字典树优化

    ---------------------

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <vector>
    
    using namespace std;
    
    char str[11111];
    char words[1111][44];
    int f[11111];
    int _cost;
    int n;
    int pot;
    
    //用类,或者结构体定义都行
    struct trie
    {
        int next[26];
        int value;
        int cost;
        trie()
        {
           for(int i=0;i<26;i++) next[i]=0;
           value=0;//记录是不是一个单词
           cost=0;
        }
        void init()
        {
            memset(next,0,sizeof(next));
            value=0;
            cost=0;
        }
    }mmr[333333];
    
    //插入:
    void insert(int root,char* s,int ct)
    {
        int p=root;
        int k=0;
        while(s[k]!='\0')
        {
            if(!mmr[p].next[s[k]-'a'])
            {
                mmr[pot].init();
                mmr[p].next[s[k]-'a']=pot++;
            }
            p=mmr[p].next[s[k]-'a'];
            k++;
        }
        mmr[p].value=1;
        mmr[p].cost=ct;
    }
    
    //查找
    void find(int root,char* s,int pos)
    {
        int p=root;
        int k=0;
        while(s[k]!='\0'&&mmr[p].next[s[k]-'a'])
        {
            p=mmr[p].next[s[k]-'a'];
            if (mmr[p].value==1)
            {
                f[pos+k+1]=max(f[pos+k+1],f[pos]+mmr[p].cost);
            }
            k++;
        }
    }
    
    int main()
    {
        int l;
        int root;
        while (~scanf("%d%s",&n,str+1))
        {
            memset(f,-1,sizeof(f));
            root=1;
            mmr[root].init();
            pot=2;
            l=strlen(str+1);
            for (int i=0;i<n;i++)
            {
                scanf("%s",words[i]);
                scanf("%d",&_cost);
                insert(root,words[i],_cost);
            }
            f[0]=0;
            for (int i=1;i<=l;i++)
            {
                if (f[i-1]!=-1)
                {
                    find(root,str+i,i-1);
                }
            }
            printf("%d\n",f[l]);
        }
        return 0;
    }
    





  • 相关阅读:
    p1373
    考试总结 2018-5-6
    p1044与p1898
    p1905
    p1904 p1903
    p1177
    p1273  日常打表
    p1142
    并查集与并查集模板
    p1265
  • 原文地址:https://www.cnblogs.com/cyendra/p/3226336.html
Copyright © 2011-2022 走看看