2010辽宁省赛F(字典树,动态规划)

#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数组中,第一维表示父节点,第二维表示兄弟节点。每个节点挂一个链表,把它后面的节点连起来,对于两个串的最长公共前缀的长度即他们所在的结点的公共祖先个数。

原文地址:https://www.cnblogs.com/ldudxy/p/9417338.html

时间: 2024-11-15 16:29:07

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

2010辽宁省赛 NBUT 1222 English Game【字典树+DP】

[1222] English Game 时间限制: 1000 ms 内存限制: 131072 K 链接:Click Here! 问题描述 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 wei

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 enough money to run a hotel. The best for him is that he need not go to work outside, just wait for the money to go into his pocket. Little A wants everyth

2010辽宁省赛E(Bellman_Ford最短路,状态压缩DP【三进制】)

#include<bits/stdc++.h>using namespace std;const int inf=0x3f3f3f3f;struct node{    int v,z,d,next;//存可以连接的点,用next存邻接表}a[10010];struct road{    int u,cnt,dis;//dis储存当前需要的钱数,即最短路算法里的权,u储存顶点,cnt储存组合数即状态压缩dp    road(int uu,int cntt,int diss)    {      

[ACM]辽宁省赛2010 (HZNU 1081-1089)

虽然退役了,但偶尔水几题醒醒脑还是不错的=_= 1085 Intermediary 暂时还没做 1081: Dinner 时间限制: 1 Sec  内存限制: 32 MB提交: 5  解决: 3[提交][状态][讨论版] [Edit] [TestData] 题目描述 Little A is one member of ACM team. He had just won the gold in World Final. To celebrate, he decided to invite all

测试赛A - Colored Sticks(并查集+字典树+欧拉回路)

A - Colored Sticks Time Limit:5000MS     Memory Limit:128000KB     64bit IO Format:%I64d & %I64u Submit Status Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sti

【BZOJ2741】【FOTILE模拟赛】L 可持久化字典树+分块

广告: #include <stdio.h> int main() { puts("转载请注明出处[vmurder]谢谢"); puts("网址:blog.csdn.net/vmurder/article/details/44496739"); } 题解: 首先我们处理出来sum[0,n]作为异或前缀和,然后答案就不再是[l,r]中间某段区间的异或和,而转化成求了[l?1,r]中任意两点异或和的最大值. 然后我们分块处理出fi,j表示 [第i块的开头,j

北邮校赛 F. Gabriel&#39;s Pocket Money(树状数组)

F. Gabriel's Pocket Money 2017- BUPT Collegiate Programming Contest - sync 时间限制 2000 ms 内存限制 65536 KB 题目描述 For centuries, Heaven has required its young angels to live and study among humans in order to become full-fledged angels. This is no different

辽宁省赛——杨鲁斯卡尔专场 -F

题意: 题意就是输入N以EOF结尾,形成1-N的数字序列,然后选取一个幸运数字,每隔幸运数字的长度就将数字从序列中删掉, 选取幸运数字的规则是最开始是2,之后删掉数字之后每次选取序列中除1和选过的幸运数字外最小的.3<=n<=10000) 样例输入20 30 样例输出6 8 提示 eg.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 For the first time, delete the last number in every tw

2014牡丹江区域赛H(字典树)ZOJ3826

Hierarchical Notation Time Limit: 2 Seconds      Memory Limit: 131072 KB In Marjar University, students in College of Computer Science will learn EON (Edward Object Notation), which is a hierarchical data format that uses human-readable text to trans