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 就是说一个 ...
随机推荐
- 如何判断一个整数是否是2的N次幂
static bool CheckPowerOfTwo(ulong num) { && (num & (num - )) == ; }
- Entity Framework 6.X实现记录执行的SQL功能
Entity Framework在使用时,很多时间操纵的是Model,并没有写sql语句,有时候为了调试或优化等,又需要追踪Entity framework自动生成的sql(最好还能记录起来,方便出错 ...
- [翻译] USING GIT IN XCODE [5] 在XCODE中使用GIT[5]
USING GIT IN XCODE USING BRANCHES Branches can be a very effective tool to isolate new features or e ...
- Squid安装配置和使用
文:铁乐与猫 环境 centos 6.5 x64 安装 最简单的一种就是yum安装. yum install squid 版本 rpm -qa | grep squid squid-3.1.23-16 ...
- November 21st 2016 Week 48th Monday
A bird is known by its note, and a man by his talk. 闻其声而知鸟,听其言而知人. Listen to what a man talks, watch ...
- Leuze BCL308i 使用方法整理
1 硬件连接关系 1.1 接口盒 BCL308i一般选配MK308/MK348/MK358系列接口盒, 单独使用(不组成扫描集群)时需要连接3根线,分别为SERVICE .SW/PWR.HOST/BU ...
- U-Mail详解邮件营销优势及应用领域
最近频频有营销人员向U-Mail小编咨询:邮件营销到底有什么好处呢?与此同时,还有不少人对邮件营销存在一定的误解:邮件营销是不是只给潜在消费者发送邮件推广商品呢?其实邮件群发的应用面非常广泛,可不仅仅 ...
- 对象在hibernate中的状态
首先hibernate中对象的状态有三种:瞬态.游离态和持久态,三种状态转化的方法都是通过session来调用,瞬态到持久态的方法有save().saveOrUpdate().get().load() ...
- Ceph块存储介绍
1. 块存储是什么 块存储简称RBD(RADOS Block Device),是一种有序的字节序块,也是在Ceph三大存储类型中最为常用的存储方式 ,Ceph的块存储是基于RADOS的,因此它也借助R ...
- Cloudera Manager 4.6 安装部署hadoop CDH集群
Cloudera Manager 4.6 安装详解 1. Cloudera Manager介绍 1.1. 功能介绍 Cloudera Manager是一个针对hadoop集群的管理工具,功能包括:cd ...