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. ShuffleElements(随机打乱数组中的元素)

    给定一个数组,随机打乱数组中的元素,题意很简单直接上代码: package Array; import java.util.Arrays; import java.util.Collections; ...

  2. Lower Power with CPF(四)

    CPF从Front-end到Back-end(RTL--GDSII)的整个流程: 1)Creating a CPF file:来在前端就建立lower power的规范. 2)检查CPF文件的正确性, ...

  3. 74. Search a 2D Matrix(二分查找,剑指offer 1)

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  4. Linux命令: ls -F

    ls -F 列出目录中的文件 -F参数使得ls命令显示的目录文件名之后加一个斜线(“/”)字符 文件后面的星号("*")表示这是一个可执行程序

  5. Python: dict setdault函数与collections.defaultdict()的区别

    setdault用法 >>>dd={'hy':1,'hx':2} >>>cc=dd.setdefault('hz',1) >>>cc      返 ...

  6. MAVEN打包丢失xml文件解决办法

    MAVEN打包默认只包含src/main/java下的class文件,如果需要包含xml.properties等文件,请在build节点下面添加如下代码 <resources> <r ...

  7. linux常用命令:ip 命令

    ip命令是Linux下较新的功能强大的网络配置工具. 1.命令格式: ip  [OPTIONS]  OBJECT  [COMMAND [ARGUMENTS]] 2.命令功能: ip命令用来显示或操纵L ...

  8. linux常用命令:telnet 命令

    telnet命令通常用来远程登录.telnet程序是基于TELNET协议的远程登录客户端程序.Telnet协议是TCP/IP协议族中的一员,是Internet远程登陆服务的标准协议和主要方式.它为用户 ...

  9. python repr方法和str方法

    每个类都有默认的__repr__, __str__方法,用print 实例时调用类的str方法,直接输出类的实例,调用的是类的repr方法 在命令行界面,不用print命令打印而是直接写变量名,就是用 ...

  10. ACM竞赛之输入输出(以C与C++为例)

    本文转自互联网,内容.排版有修正. 欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju G ...