#include<bits/stdc++.h>
using namespace std;
int n,x;
char s[10010];
char a[31010];
int val[100010];
int ch[100010][30];
int dp[100010];
int main()
{
    while(~scanf("%d",&n))
    {
        scanf("%s",s+1);
        int len=strlen(s+1);
        memset(ch[0],0,sizeof(ch[0]));
        int cnt=0;//记录编号
        for(int i=1;i<=n;i++)
        {
            int u=0;//父节点,0为根节点
            scanf("%s%d",a,&x);
            for(int j=0;j<strlen(a);j++)
            {
                if(!ch[u][a[j]-'a'])//字典树中该位置已存在该字母
                {
                    ch[u][a[j]-'a']=++cnt;
                    memset(ch[cnt],0,sizeof(ch[cnt]));
                    val[cnt]=0;
                }
                u=ch[u][a[j]-'a'];
            }
            val[u]=max(val[u],x);//u为结尾结点的编号,val[u]表示该串的权重
        }
        memset(dp,0,sizeof(dp));
        dp[0]=1;
        for(int i=1;i<=len;i++)
        {
            int u=0;
            if(!dp[i-1])
                continue;
            for(int j=i;j<=i+30&&j<=len;j++)
            {
                if(ch[u][s[j]-'a'])
                {
                    u=ch[u][s[j]-'a'];
                    if(val[u])//如果有以u结尾的字符串,检验它的权重加上i结束的字符串的权重是否比原来更大
                        dp[j]=max(dp[j],dp[i-1]+val[u]);
                }
                else
                    break;
            }
        }
    //模拟从开始到完成字符串加入进行匹配的过程
        printf("%d\n",dp[len]-1);
    }
    return 0;
}
//字典树是一种以空间换时间的数据结构,在ch数组中,第一维表示父节点,第二维表示兄弟节点。每个节点挂一个链表,把它后面的节点连起来,对于两个串的最长公共前缀的长度即他们所在的结点的公共祖先个数。

2010辽宁省赛F(字典树,动态规划)的更多相关文章

  1. NBUT 1221 Intermediary 2010辽宁省赛

    Time limit 1000 ms Memory limit 131072 kB It is widely known that any two strangers can get to know ...

  2. NBUT 1218 You are my brother 2010辽宁省赛

    Time limit 1000 ms Memory limit 131072 kB Little A gets to know a new friend, Little B, recently. On ...

  3. 2019icpc南京网络赛 F 主席树

    题意 给一个\(n\)的全排列数组\(a\),求一个递推数组每一项的值:\(ans[i]=ans[j]+1\),\(j\)为\(a[pos[i]-k]到a[pos[i]+k],(pos[i]为i在数组 ...

  4. NBUT 1224 Happiness Hotel 2010辽宁省赛

    Time limit 1000 ms Memory limit 131072 kB The life of Little A is good, and, he managed to get enoug ...

  5. ZOJ 1985 Largest Rectangle in a Histogram(刷广告)2010辽宁省赛

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21204 ...

  6. NBUT 1222 English Game 2010辽宁省赛

    Time limit 1000 ms Memory limit 131072 kB This English game is a simple English words connection gam ...

  7. NBUT 1225 NEW RDSP MODE I 2010辽宁省赛

    Time limit  1000 ms Memory limit  131072 kB Little A has became fascinated with the game Dota recent ...

  8. NBUT 1220 SPY 2010辽宁省赛

    Time limit  1000 ms Memory limit  131072 kB The National Intelligence Council of X Nation receives a ...

  9. NBUT 1219 Time 2010辽宁省赛

    Time limit   1000 ms Memory limit   131072 kB Digital clock use 4 digits to express time, each digit ...

随机推荐

  1. spring boot: 在maven中装入springframework框架

    1.在maven 的pom.xml中加入 <dependency> <groupId>org.springframework</groupId> <artif ...

  2. django使用bootstrap前端框架

    一.下载bootstrap相关文件,放在项目目录中.在blog 应用中新建static目录,bootstrap文件放在此目录下. bootstrap下载网址:http://v3.bootcss.com ...

  3. Python基础之列表操作

    列表的常用操作包括但不限于以下操作: 列表的索引,切片,追加,删除,切片等 这里将对列表的内置操作方法进行总结归纳,重点是以示例的方式进行展示. 使用type获取创建对象的类 type(list) 使 ...

  4. linux命令学习笔记(55):traceroute命令

    通过traceroute我们可以知道信息从你的计算机到互联网另一端的主机是走的什么路径.当然每次数据包 由某一同样的出发点(source)到达某一同样的目的地(destination)走的路径可能会不 ...

  5. do-while-zero 结构在宏定义中的应用

    do while 语句在使用宏定义时是一个有用的技巧,说明如下: 假设有这样一个宏定义 #define macro(condition) / if(condition) dosomething() 现 ...

  6. G 唐纳德与子串(easy)(华师网络赛---字符串,后缀数组)(丧心病狂的用后缀自动机A了一发Easy)

    Time limit per test: 1.0 seconds Memory limit: 256 megabytes 子串的定义是在一个字符串中连续出现的一段字符.这里,我们使用 s[l…r] 来 ...

  7. 2017-2018-1 20179203 《Linux内核原理与分析》第四周作业

    攥写人:李鹏举 学号:20179203 ( 原创作品转载请注明出处) ( 学习课程:<Linux内核分析>MOOC课程http://mooc.study.163.com/course/US ...

  8. ACM学习历程—HDU 5512 Pagodas(数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...

  9. bzoj 2823: [AHOI2012]信号塔 最小圆覆盖

    题目大意: 给定n个点,求面积最小的园覆盖所有点.其中\(n \leq 10^6\) 题解: 恩... 刚拿到这道题的时候... 什么???最小圆覆盖不是\(O(n^3)\)的随机增量算法吗????? ...

  10. 【转】Pro Android学习笔记(十一):了解Intent(中)

    Intent的构成 Intent可以带有action,data(由URI表达),extra data(key/value map,键值对),指定的类名(成为component name).一个inte ...