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的子串(可以不连续)相同的个数. 思路:在 ...
随机推荐
- 关于 WebBrowser调用百度地图API 鼠标滚轮缩放地图级别失灵的解决办法
在桌面程序下 百度地图API的鼠标缩放地图功能可能会失灵无效! 这个原因不是API的问题 小弟试了下在WEB上面是没有问题的 于是考虑可能是WebBrowser的获取焦点问题,于是在主窗体 添加了一个 ...
- 76. Minimum Window Substring(hard 双指针)
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- select,radio,checkbox兼容性
- 持续集成之二:搭建SVN服务器(SvnAdmin)
安装环境 Red Hat Enterprise Linux Server release 7.3 (Maipo) jdk1.7.0_80 apache-tomcat-7.0.90 mysql-5.7. ...
- python选择排序算法总结
选择排序算法: a=[6,5,4,3,2,1] 算法思路: 第一步:在列表的第一个位置存放此队列的最小值 声明一个变量min_index等于列表的第一个坐标值0 从第一个位置0坐标开始,和它后边所有的 ...
- B轮公司技术问题列表
B轮公司技术问题列表 1.异构系统的接口对接我们有自己的一套统一接口,但是需要与其它公司的接口做对接,但是各个公司的接口各不相同,有什么好的方式能够方便与各公司的接口做对接的同时我们这边也能尽量少或者 ...
- 20145318《网络对抗》注入shellcode及Return-to-libc
20145318<网络对抗>注入shellcode及Return-to-libc 注入shellcode 知识点 注入shellcodeShellcode实际是一段代码(也可以是填充数据) ...
- vijos 1096 津津的储存计划
题目描述 Description 津津的零花钱一直都是自己管理.每个月的月初妈妈给津津300元钱,津津会预算这个月的花销,并且总能做到实际花销和预算的相同. 为了让津津学习如何储蓄,妈妈提出,津津可以 ...
- C# 图片和64位编码的转换
/* 将图片转换为64位编码 */ //找到文件夹 System.IO.DirectoryInfo dd = new System.IO.DirectoryInfo("C://qq" ...
- Python3基础 if嵌套示例
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...