#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. node.js+express+jade系列三:404错误的配置

    1:新建一个404.jade 2:在app.js后面配置如下代码 app.use(function(req, res){        res.render("404", {sta ...

  2. ES _all、_source的使用——_all字段连接所有字段的值构成一个用空格(space)分隔的大string而被analyzed和index,document主体保存在_source中

    1._all 1.1_all field _all字段是一个很少用到的字段,它连接所有字段的值构成一个用空格(space)分隔的大string,该string被analyzed和index,但是不被s ...

  3. rust ownership 系统

    ### 对象销毁规则 未被使用的函数返回值 被let绑定的值, 在函数末尾销毁,除非被moved ``` let v = obj::new("a"); other_fun(v); ...

  4. web网页打印的方法(浏览器通用)

    Web打印组件jatoolsPrinter 应用web化,不论对开发商,还是对用户来说,实在是一种很经济的选择,因为基于 web的应用,客户端的规则很简单,容易学习,容易维护,容易发布.但对程序员来说 ...

  5. 利用perlin noise 生成 wood texture

    %%% Perlin Noise %%% Wood_texture clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image P ...

  6. Debian for ARM install python 3.5.x

    /********************************************************************************** * Debian for ARM ...

  7. Poj1050_To the Max(二维数组最大字段和)

    一.Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is an ...

  8. virtual judge(专题一 简单搜索 A)

    问题描述: Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘, ...

  9. jquery easyui 推荐博客 (MVC+EF+EasyUI+Bootstrap)

    构建ASP.NET MVC5+EF6+EasyUI 1.4.3+Unity4.x注入的后台管理系统(52)-美化EasyUI皮肤和图标   系列目录 我很久以前就想更新系统的皮肤功能,Easyui 自 ...

  10. LAMP 2.0Apache日志切割

    每次访问网站就会产生若干条日志,当然前提是已经配置了日志. 配置日志的文件在 vim /usr/local/apache2/conf/extra/httpd-vhosts.conf 把注释掉的这两行打 ...