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的子串(可以不连续)相同的个数. 思路:在 ...
随机推荐
- Leetcode: Reorder List && Summary: Reverse a LinkedList
Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do th ...
- 剑指offer2
请实现一个函数,将一个字符串中的字符串空格替换成“%20”.例如:“We Are Happy”转化后为“We%20Are%20Happy” 思路:把字符串转化成字符数组,判断这个字符是不是空格,如果是 ...
- Java有序数组的实现
package 有序数组; public class OrdArray { private long[]array; private int nElems; //初始化 public OrdArray ...
- 有意思的JSON.parse()、JSON.stringify()
前言 现在JSON格式在web开发中非常重要,特别是在使用ajax开发项目的过程中,经常需要将后端响应的JSON格式的字符串返回到前端,前端解析成JS对象值(JSON 对象),再对页面进行渲染. 在数 ...
- 谷歌插件--Advanced REST client
早上在测试调用服务去获取数据的时候,因为自己的单元测试不是很熟悉,问了同事,同事给我介绍了一个插件Advanced REST client,这个可以在谷歌的“扩展与应用”中找打,使用 安装之后会提示要 ...
- python pip list 命令列出所有安装包和版本信息
c:\Python27\Scripts>pip listDEPRECATION: The default format will switch to columns in the future. ...
- python管道pipe,两个进程,使用管道的两端分别执行写文件动作,带锁(lock)
#coding=utf-8import multiprocessing as mp def write_file(content,lock): lock.acquire() with op ...
- Maven的scope的值
Maven的依赖范围 在pom.xml文件中,有个元素是scope,用来表示依赖的范围.之所以会有依赖范围,是因为Maven在编译.测试和运行项目时会各自使用一套classpath,依赖范围就是用来控 ...
- C/C++之面向对象
面向对象的三个基本特征(讲解) 面向对象的三个基本特征是:封装.继承.多态. 封装 封装最好理解了.封装是面向对象的特征之一,是对象和类概念的主要特性. 封装,也就是把客观事物封装成抽象的类,并且类可 ...
- JavaScript 实现表格隔行变色
JavaScript 实现表格隔行变色 版权声明:未经授权,严禁分享! 构建界面 界面HTML代码 <style> #data,th,td{ border: 1px solid #aaaa ...