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 ...
随机推荐
- node.js+express+jade系列三:404错误的配置
1:新建一个404.jade 2:在app.js后面配置如下代码 app.use(function(req, res){ res.render("404", {sta ...
- ES _all、_source的使用——_all字段连接所有字段的值构成一个用空格(space)分隔的大string而被analyzed和index,document主体保存在_source中
1._all 1.1_all field _all字段是一个很少用到的字段,它连接所有字段的值构成一个用空格(space)分隔的大string,该string被analyzed和index,但是不被s ...
- rust ownership 系统
### 对象销毁规则 未被使用的函数返回值 被let绑定的值, 在函数末尾销毁,除非被moved ``` let v = obj::new("a"); other_fun(v); ...
- web网页打印的方法(浏览器通用)
Web打印组件jatoolsPrinter 应用web化,不论对开发商,还是对用户来说,实在是一种很经济的选择,因为基于 web的应用,客户端的规则很简单,容易学习,容易维护,容易发布.但对程序员来说 ...
- 利用perlin noise 生成 wood texture
%%% Perlin Noise %%% Wood_texture clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image P ...
- Debian for ARM install python 3.5.x
/********************************************************************************** * Debian for ARM ...
- Poj1050_To the Max(二维数组最大字段和)
一.Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is an ...
- virtual judge(专题一 简单搜索 A)
问题描述: Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘, ...
- jquery easyui 推荐博客 (MVC+EF+EasyUI+Bootstrap)
构建ASP.NET MVC5+EF6+EasyUI 1.4.3+Unity4.x注入的后台管理系统(52)-美化EasyUI皮肤和图标 系列目录 我很久以前就想更新系统的皮肤功能,Easyui 自 ...
- LAMP 2.0Apache日志切割
每次访问网站就会产生若干条日志,当然前提是已经配置了日志. 配置日志的文件在 vim /usr/local/apache2/conf/extra/httpd-vhosts.conf 把注释掉的这两行打 ...