2010辽宁省赛F(字典树,动态规划)
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;
}
}
}
return 0;
}
2010辽宁省赛F(字典树,动态规划)的更多相关文章
- NBUT 1221 Intermediary 2010辽宁省赛
Time limit 1000 ms Memory limit 131072 kB It is widely known that any two strangers can get to know ...
- 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 ...
- 2019icpc南京网络赛 F 主席树
题意 给一个\(n\)的全排列数组\(a\),求一个递推数组每一项的值:\(ans[i]=ans[j]+1\),\(j\)为\(a[pos[i]-k]到a[pos[i]+k],(pos[i]为i在数组 ...
- 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 ...
- ZOJ 1985 Largest Rectangle in a Histogram(刷广告)2010辽宁省赛
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21204 ...
- NBUT 1222 English Game 2010辽宁省赛
Time limit 1000 ms Memory limit 131072 kB This English game is a simple English words connection gam ...
- 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 ...
- NBUT 1220 SPY 2010辽宁省赛
Time limit 1000 ms Memory limit 131072 kB The National Intelligence Council of X Nation receives a ...
- NBUT 1219 Time 2010辽宁省赛
Time limit 1000 ms Memory limit 131072 kB Digital clock use 4 digits to express time, each digit ...
随机推荐
- el表达式判断字符串相等
el表达式判断字符串相等 Java code 1 ${"a" == "a"} ${"b" eq "b"} 都可以 & ...
- C#返回Json,js解析Json,并添加到select标签中
后台: List<Student> list=GetAll();//id name string json = new JavaScriptSerializer().Serialize(l ...
- Unity3D之Mesh(三)绘制四边形
前言: 由於之前的基本介紹,所以有關的知識點不做贅述,只上案例,知識作爲自己做試驗的記錄,便於日後查看. 步驟: 1.創建一個empty 的gameobject: 2.添加一個脚本給這個game ob ...
- QToolBox
QToolBox类似与以前qq好友分组的那种控件.每个分组是一个Item. 一.添加分组: 其中每个分组是通过一下函数添加的: int addItem(QWidget * w, const QIcon ...
- 【leetcode刷题笔记】LRU Cache
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- Access中复制表
很多时候在Access中需要复制表,或只复制结构,源表名:a: 新表名:b (经测试中Access可用) 法一:select * into b from a where 1<>1 ...
- gulp之压缩图片
//先全局安装gulp:npm install -g gulp //然后在项目根目录中安装gulp依赖:npm install --save-dev gulp //http://www.gulpjs. ...
- nodejs 上传图片(服务端输出全部代码)
下面代码,全部都是nodejs端的,不用客户端代码.也就是,选择图片的form表单以及上传完毕预览图片的html,都是由node服务端输出的. 1 启动代码:(node upload.js) var ...
- bzoj 2125 最短路——仙人掌两点间最短路
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2125 因为看了TJ又抄了标程,现在感觉还是轻飘飘的……必须再做一遍. 两点间的情况: 1.直 ...
- bzoj 4066 & bzoj 2683 简单题 —— K-D树(含重构)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4066 https://www.lydsy.com/JudgeOnline/problem.p ...