Codeforces 919D - Substring
思路:
拓扑排序判环+DAG上dp+记忆化搜索
状态:dp[i][j]表示以i为起点的路径中j的最大出现次数
初始状态:dp[i][j]=1(i have no son && w[i]==j)
dp[i][j]=0(i have no son && w[i]!=j)
状态转移:dp[i][j]=max(dp[t][j])(t is i's son)
dp[i][j]++(w[i]==j)
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a)) const int N=3e5+;
int head[N];
int w[N];
int in[N];
int tin[N];
int dp[N][];
int cnt=;
struct edge{
int to,next;
}edge[N];
void add_edge(int u,int v){
edge[cnt].to=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
bool topo_sort(int n){
queue<int>q;
int cnt=;
for(int i=;i<=n;i++)if(tin[i]==)q.push(i),cnt++;
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=head[u];~i;i=edge[i].next){
tin[edge[i].to]--;
if(tin[edge[i].to]==)q.push(edge[i].to),cnt++;
}
}
return cnt>=n;
}
int dfs(int u,int x){
if(dp[u][x]!=-)return dp[u][x];
dp[u][x]=;
for(int i=head[u];~i;i=edge[i].next){
dp[u][x]=max(dp[u][x],dfs(edge[i].to,x));
}
if(w[u]==x)dp[u][x]++;
return dp[u][x];
}
int main(){
ios::sync_with_stdio(false);
cin.tie();
mem(head,-);
mem(dp,-);
int n,m,u,v;
string s;
cin>>n>>m;
cin>>s;
for(int i=;i<s.size();i++)w[i+]=s[i]-'a';
for(int i=;i<m;i++)cin>>u>>v,add_edge(u,v),in[v]++,tin[v]++;
if(topo_sort(n)){
int ans=;
for(int i=;i<=n;i++){
if(in[i]==){
for(int j=;j<;j++){
ans=max(ans,dfs(i,j));
}
}
}
cout<<ans<<endl;
}
else cout<<-<<endl;
return ;
}
Codeforces 919D - Substring的更多相关文章
- Codeforces 919D Substring (拓扑排序+树形dp)
题目:Substring 题意:给你一个有向图, 一共有n个节点 , m条变, 一条路上的价值为这个路上出现过的某个字符最多出现次数, 现求这个最大价值, 如果价值可以无限大就输出-1. 题解:当这个 ...
- Codeforces 919D Substring 【拓扑排序】+【DP】
<题目链接> 题目大意:有一个具有n个节点,m条边的有向图,每个点对应一个小写字母,现在给出每个顶点对应的字母以及有向边的连接情况,求经过的某一条路上相同字母出现的最多次数.如果次数无限大 ...
- CodeForces - 919D Substring (拓扑排序+dp)
题意:将一个字符串上的n个字符视作点,给出m条有向边,求图中路径上最长出现的相同字母数. 分析:首先如果这张图中有环,则可以取无限大的字符数,在求拓扑排序的同时可以确定是否存在环. 之后在拓扑排序的结 ...
- Codeforces 919D Substring (拓扑图DP)
手动博客搬家: 本文发表于20180716 10:53:12, 原地址https://blog.csdn.net/suncongbo/article/details/81061500 给定一个\(n\ ...
- Codeforces 919D Substring ( 拓扑排序 && DAG上的DP )
题意 : 给出含有 N 个点 M 条边的图(可能不连通或者包含环),每个点都标有一个小写字母编号,然后问你有没有一条路径使得路径上重复字母个数最多的次数是多少次,例如图上有条路径的顶点标号顺序是 a ...
- Substring CodeForces - 919D
http://codeforces.com/problemset/problem/919/D 就是先判环,如果有环就-1,否则对每个字母分开跑一下dp 错误记录: 1.有向图判环,自环一定要特判!(不 ...
- Codeforces 919D:Substring(拓扑排序+DP)
D. Substring time limit: per test3 seconds memory limit: per test256 megabytes inputstandard: input ...
- CodeForces 163A Substring and Subsequence dp
A. Substring and Subsequence 题目连接: http://codeforces.com/contest/163/problem/A Description One day P ...
- Codeforces 163A Substring and Subsequence
http://codeforces.com/problemset/problem/163/A?mobile=true 题目大意:给出两个串,问a的连续子串和b的子串(可以不连续)相同的个数. 思路:在 ...
随机推荐
- Impala与Hive的比较
1. Impala架构 Impala是Cloudera在受到Google的Dremel启发下开发的实时交互SQL大数据查询工具,Impala没有再使用缓慢的Hive+MapReduce批 ...
- WSDL解析
背景 前面我们介绍过利用javassist动态生成webservice,这种方式可以使得我们系统通过页面配置动态发布webservice服务,做到0代码开发发布北向接口.进一步思考,我们如何0代码开发 ...
- JSP禁用缓存常用方法
内容主要转自:http://www.cnblogs.com/linjiqin/archive/2011/07/20/2111627.html jsp页面禁止缓存设置 1.客户端缓存要在<head ...
- js dom 操作技巧
1.创建元素 创建元素:document.createElement() 使用document.createElement()可以创建新元素.这个方法只接受一个参数,即要创建元素的标签名.这个标签名在 ...
- 【读书笔记】SpringBoot读书笔记
整体目录结构: 一.入门 二.开发第一个应用程序 三.自定义配置 四.测试 五.Groovy与Spring Boot Cli 六.在Spring Boot中使用Grails 七.深入Actuator ...
- CentOS 7 安装OpenCV
CentOS 7 安装OpenCV步骤如下: 1.在CentOS 7命令行中直接在线安装: yum install numpy opencv* 2.安装完成后进行全盘搜索:find / -n ...
- python中hasattr, getattr,setattr及delattr四个方法
通过一个实例来说明,这四个函数的用法: 首先一个如下的一个简单的类: class Animal(object): def __init__(self,name, zone): self.name = ...
- P4180 【模板】严格次小生成树[BJWC2010]
P4180 [模板]严格次小生成树[BJWC2010] 倍增(LCA)+最小生成树 施工队挖断学校光缆导致断网1天(大雾) 考虑直接枚举不在最小生成树上的边.但是边权可能与最小生成树上的边相等,这样删 ...
- c++中类似于java jprofiler/eclispe memoryanalysis的性能以及内存分析工具
visual studio有自带的,可以看MSDN,不过一般来说,我们比较关注linux下的,搜了下,比较好用的应该有gprof和valgrind,先记录,可参考如下: http://blog.csd ...
- linux内核分析 第五周
一.实验相关 1.下载老师最新的menu文件,并在其中添加上周所编写的代码,并运行 下载 添加 运行 2.gdb调试跟踪 gdb设置跟踪文件(先进入linux-3.18.6所在的文件) gdb设置断点 ...