CF 919 D. Substring
D. Substring
题意:
在一张有向图中,定义路径的权值为路径中出现次数最多的字符出现的次数,求一条权值最大的路径。如果权值可以无限大,输出-1。
分析:
注意是一张有向图。如果存在环那么输出-1,否则枚举字符,dp一下。
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ;
struct Edge { int to, nxt; } e[N];
int f[N], head[N], deg[N], q[N], d[N], En;
char s[N];
bool vis[N]; void add_edge(int u,int v) {
++En; e[En].to = v, e[En].nxt = head[u]; head[u] = En;
deg[v] ++;
} int main() {
int n = read(), m = read();
scanf("%s", s + );
for (int i = ; i <= m; ++i) {
int u = read(), v = read();
add_edge(u, v);
}
int L = , R = ;
for (int i = ; i <= n; ++i) {
d[i] = deg[i];
if (!deg[i]) q[++R] = i;
}
while (L <= R) {
int u = q[L ++];
vis[u] = ;
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (!(--d[v])) q[++R] = v;
}
}
for (int i = ; i <= n; ++i)
if (!vis[i]) { puts("-1"); return ; }
int ans = ;
for (int c = 'a'; c <= 'z'; ++c) {
L = , R = ;
for (int i = ; i <= n; ++i) {
f[i] = ;
d[i] = deg[i];
if (!deg[i]) {
q[++R] = i;
if (s[i] == c) f[i] = ;
}
}
while (L <= R) {
int u = q[L ++];
ans = max(ans, f[u]);
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
f[v] = max(f[v], f[u] + (s[v] == c)); // !!!
if (!(--d[v])) q[++R] = v;
}
}
}
cout << ans;
return ;
}
CF 919 D. Substring的更多相关文章
- Codeforces 919 D Substring
题目描述 You are given a graph with nn nodes and mm directed edges. One lowercase letter is assigned to ...
- [cf 1015f] Bracket Substring (dp+kmp)
传送门 Solution 设dp方程dp[now][pos][red][fla]表示还有now个位置,pos表示匹配到第几位,red表示左括号数-右括号数,fla表示是否已经是给定串的字串 暴力转移即 ...
- 做题记录 To 2019.2.13
2019-01-18 4543: [POI2014]Hotel加强版:长链剖分+树形dp. 3653: 谈笑风生:dfs序+主席树. POJ 3678 Katu Puzzle:2-sat问题,给n个变 ...
- kmp练习
kmp板子如下, 失配数组不优化的话, $f_i$就表示子串[0...i]前后缀最大匹配长度 int main() { scanf("%s%s", t, p); int n = s ...
- 【Cf edu 30 B. Balanced Substring】
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- CF #579 (Div. 3) D1.Remove the Substring (easy version)
D1.Remove the Substring (easy version) time limit per test2 seconds memory limit per test256 megabyt ...
- CF873B Balanced Substring (前缀和)
CF873B Balanced Substring (前缀和) 蛮有意思的一道题,不过还是.....................因为CF评测坏了,没有试过是否可过. 显然求\(\sum[i][0] ...
- CF #575 Div3
// 比赛链接:https://codeforces.com/contest/1196 // CF 2019.7.24 // 本想Div3手速场上分,结果卡在C题,掉了不少分. // 自闭了这么久,今 ...
- CF 题目选做
写省选的题目对noip没什么大用 关键是 细节题或者是思考题比较重要 练思维自然是CF比较好了 把我见到的比较好的CF题放上来刷一刷. LINK:Complete the projects 就是说一个 ...
随机推荐
- 使用Reflector反编译并提取源代码
Reflector是一个强大的.net 反编译工具,有时我们不止需要反编译源代码,更需要提取源代码. Reflector本身不自带提取源代码功能,不过可以借助插件Reflector.FileDisas ...
- Gerrit安装配置
环境: CentOS 1611 + gerrit-2.11.4 (review.openstack.org) 1. 安装java1.8 (>1.7) [root@review ~]# yum i ...
- 导出Excel 2007 (NPOI)
今天在导出Excel2007时报了个错,问是否修复,点yes就提示修复正常了,但具体什么原因没说,如图 之前简单的导出代码是这样写的 public static void ExportToWeb(st ...
- 树&二叉树&&满二叉树&&完全二叉树&&完满二叉树
目录 树 二叉树 完美二叉树(又名满二叉树)(Perfect Binary Tree) 完全二叉树(Complete Binary Tree) 完满二叉树(Full Binary Tree) 树 名称 ...
- TCP socket和web socket的区别
小编先习惯性的看了下某中文百科网站对Web Socket的介绍,觉得很囧.如果大家按照这个答案去参加BAT等互联网公司的前端开发面试,估计会被鄙视. 还是让我们阅读一些英文材料吧. 让我们直接看sta ...
- Array.prototype.reduce 的理解与实现
Array.prototype.reduce 是 JavaScript 中比较实用的一个函数,但是很多人都没有使用过它,因为 reduce 能做的事情其实 forEach 或者 map 函数也能做,而 ...
- jquery根据字符切割字符串
var str=new String(); var arr=new Array(); str="ddd,dsd,3,dd,g,k"; //可以用字符或字符串分割 arr=str.s ...
- gitlab+jenkins环境搭建.md
gitlab+jenkins自动化部署环境搭建 环境说明 系统 主机 IP 安装软件 CentOS 7 study-1 192.168.100.51 gitlab.git CentOS 7 study ...
- SOJ1022 Uniform Generator
Computer simulations often require random numbers. One way to generate pseudo-random numbers is via ...
- JavaScript组合继承的一点思考
今天看<JavaScript高级程序设计>一书中关于组合继承模式时.书上有这么一个Demo程序: <html> <head> </head> <b ...