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的更多相关文章

  1. Codeforces 919D Substring (拓扑排序+树形dp)

    题目:Substring 题意:给你一个有向图, 一共有n个节点 , m条变, 一条路上的价值为这个路上出现过的某个字符最多出现次数, 现求这个最大价值, 如果价值可以无限大就输出-1. 题解:当这个 ...

  2. Codeforces 919D Substring 【拓扑排序】+【DP】

    <题目链接> 题目大意:有一个具有n个节点,m条边的有向图,每个点对应一个小写字母,现在给出每个顶点对应的字母以及有向边的连接情况,求经过的某一条路上相同字母出现的最多次数.如果次数无限大 ...

  3. CodeForces - 919D Substring (拓扑排序+dp)

    题意:将一个字符串上的n个字符视作点,给出m条有向边,求图中路径上最长出现的相同字母数. 分析:首先如果这张图中有环,则可以取无限大的字符数,在求拓扑排序的同时可以确定是否存在环. 之后在拓扑排序的结 ...

  4. Codeforces 919D Substring (拓扑图DP)

    手动博客搬家: 本文发表于20180716 10:53:12, 原地址https://blog.csdn.net/suncongbo/article/details/81061500 给定一个\(n\ ...

  5. Codeforces 919D Substring ( 拓扑排序 && DAG上的DP )

    题意 : 给出含有 N 个点 M 条边的图(可能不连通或者包含环),每个点都标有一个小写字母编号,然后问你有没有一条路径使得路径上重复字母个数最多的次数是多少次,例如图上有条路径的顶点标号顺序是  a ...

  6. Substring CodeForces - 919D

    http://codeforces.com/problemset/problem/919/D 就是先判环,如果有环就-1,否则对每个字母分开跑一下dp 错误记录: 1.有向图判环,自环一定要特判!(不 ...

  7. Codeforces 919D:Substring(拓扑排序+DP)

    D. Substring time limit: per test3 seconds memory limit: per test256 megabytes inputstandard: input ...

  8. CodeForces 163A Substring and Subsequence dp

    A. Substring and Subsequence 题目连接: http://codeforces.com/contest/163/problem/A Description One day P ...

  9. Codeforces 163A Substring and Subsequence

    http://codeforces.com/problemset/problem/163/A?mobile=true 题目大意:给出两个串,问a的连续子串和b的子串(可以不连续)相同的个数. 思路:在 ...

随机推荐

  1. Summary: Stack Overflow Error

    What is a stack overflow error? Parameters and local variables are allocated on the stack (with refe ...

  2. SSH无密码登录:只需两个简单步骤 (Linux)

    最后更新 2017年4月8日 分类 最新文章 服务器安全 标签 RSA SSH Key 非对称加密 如果你管理一台Linux服务器,那么你就会知道每次SSH登录时或者使用scp复制文件时都要输入密码是 ...

  3. ftp.GetResponse() 无法连接到远程服务器

    最近在做一个ftp上传下载以及在服务器上创建文件夹的工具 报 GetResponse() 无法连接到远程服务器  错误 明明 ip , 账户和 密码 用ftp 工具都能连接上 ,可是 代码就不行了,看 ...

  4. zw版【转发·台湾nvp系列Delphi例程】HALCON BitXor

    zw版[转发·台湾nvp系列Delphi例程]HALCON BitXor procedure TForm1.Button1Click(Sender: TObject);var image0, imag ...

  5. ArcGIS 10——版本编辑流程

    上一篇文章学习了ArcGIS有关版本机制实现的基本原理,本文结合ArcGIS的数据编辑知识来将版本编辑.协调.解决冲突.提交更改的整个过程加以说明. 同上篇文章一样,写作本文的初始意图是因为目前的项目 ...

  6. JSON草稿

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  7. 在win和android上同时进行OpenCV程序设计

    基于qt进行Android图像处理项目设计的时候,初期可以首先在windows上进行调试,等到比较成熟后将代码转换到adnroid上. 这里仅以widget为例,如果使用qtquick是一样的.具体以 ...

  8. python面向对象总结!

    面向对象 Object Oriented Programming 基本单元:对象把数据和功能封装在里边,能实现很好的复用性,灵活性和扩展性. 面向对象的两个基本概念:类和对象 面向对象的基本要素:属性 ...

  9. vi如何修改注释颜色

    答:往~/.vimrc或/etc/vimrc的最后添加以下行: hi comment ctermfg=6

  10. 【环境搭建】CDH版Hadoop环境搭建

    1.下载组件 首先去CDH网站上下载hadoop组件 地址:http://archive.cloudera.com/cdh5/cdh/5/ 注意版本号要与其他的组件CDH版本一致 2.环境配置 设置主 ...