Codeforces 919D - Substring
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+5; int head[N]; int w[N]; int in[N]; int tin[N]; int dp[N][26]; int cnt=0; 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=0;for(int i=1;i<=n;i++)if(tin[i]==0)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]==0)q.push(edge[i].to),cnt++;}}return cnt>=n; } int dfs(int u,int x){if(dp[u][x]!=-1)return dp[u][x];dp[u][x]=0;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(0);mem(head,-1);mem(dp,-1);int n,m,u,v;string s;cin>>n>>m;cin>>s;for(int i=0;i<s.size();i++)w[i+1]=s[i]-'a';for(int i=0;i<m;i++)cin>>u>>v,add_edge(u,v),in[v]++,tin[v]++;if(topo_sort(n)){int ans=0;for(int i=1;i<=n;i++){if(in[i]==0){for(int j=0;j<26;j++){ans=max(ans,dfs(i,j));}}}cout<<ans<<endl;}else cout<<-1<<endl;return 0; }
转载于:https://www.cnblogs.com/widsom/p/8399193.html
Codeforces 919D - Substring相关推荐
- Codeforces 919D Substring (拓扑图DP)
Codeforces 919D Substring (拓扑图DP) 手动博客搬家: 本文发表于20180716 10:53:12, 原地址https://blog.csdn.net/suncongbo ...
- codeforces Balanced Substring
B. Balanced Substring time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Codeforces 题目合集+分类+代码 【Updating...】【361 in total】
961A - Tetris 模拟 ...
- 【学习笔记】有向无环图上的DP
[学习笔记]有向无环图上的DP 手动博客搬家: 本文发表于20180716 10:49:04, 原地址https://blog.csdn.net/suncongbo/article/details/8 ...
- Codeforces Round #590 (Div. 3) F. Yet Another Substring Reverse 子集dp
传送门 文章目录 题意: 思路: 题意: 思路: 之前做过类似的题,翻转一个字串相当于将任意两个不相交的串连在一起.再一看字符集≤20\le20≤20,那就是铁子集dpdpdp了. 定义f[i]f[i ...
- Balanced Substring CodeForces - 873B (思维+前缀和)
Balanced Substring CodeForces - 873B You are given a string s consisting only of characters 0 and 1. ...
- [题解]RGB Substring (hard version)-前缀和(codeforces 1196D2)
题目链接:https://codeforces.com/problemset/problem/1196/D2 题意: q 个询问,每个查询将给你一个由 n 个字符组成的字符串s,每个字符都是 &quo ...
- Codeforces 1015F Bracket Substring AC自动机 + dp
Bracket Substring 这么垃圾的题怎么以前都不会写啊, 现在一眼怎么就会啊.... 考虑dp[ i ][ j ][ k ][ op ] 表示 已经填了 i 个空格, 末尾串匹配到 所给串 ...
- Codeforces 1196D RGB Substring
(easy version) https://codeforces.com/contest/1196/problem/D1 (hard version) https://codeforces.com/ ...
最新文章
- 基于YOLOv5的智慧工地实现---安全帽检测(2)
- 60+应用,哪款是你最爱?
- TAPI 电话应用程序接口
- 多功能节点连线绘图控件Nevron Diagram for .NET使用方法及下载地址
- tc溜溜865手机投屏卡_溜溜tcgames老版本(电脑玩手机游戏)-溜溜TC Games32位/64位旧版本PC下载V2.0.0官网安卓真机投屏-西西软件下载...
- 安卓弹性刷新通用版—支持任何view上下刷新均有弹性效果
- WebsResponse
- 为域用户创建漫游用户配置文件
- 使用jeDate日期控件
- 500 G JAVA视频网盘分享(JEECG开源社区)
- ArcGIS Model批量矢量裁剪矢量
- php中怎么获取提交时间,php – 通过curl_getinfo()获取详细信息来获取cURL请求的时间...
- android开发中Fingerprint模块浅析
- 微信H5链接唤起三方App总结
- 黑苹果虚拟机——显卡只有7M玩玩就行
- 可汗学院公开课:统计学笔记——中心极限定理、置信区间
- 国际知名制作公司名录及网址大全,制作人员必备
- android 同根动画_[转载]Android anim动画切换效果
- 关于环境依赖问题的反思NO.1
- Unable to start program bug解决方法
热门文章
- RTX51 Tiny中容易混淆的问题
- sql2008 查询某个表被那些存储过程用到
- SQL Server 2000+ADO.NET实现并发控制
- 关闭 VSCode 自动更新
- 倩女手游服务器维护,倩女手游7月28日服务器在线维护公告
- suparc服务器没信号,[SupARC平台常见问题帮助](2012-09-19更新)
- 机器人学习--电子指南针定位导航
- Verilog功能模块——串行数据转并行数据
- 计算机视觉与深度学习 | 不含动态背景的前景目标提取
- php 微信公众号 记录数据库,PHP构建微信公众号关键字数据库多图文回复