Codeforces 919 D Substring
题目描述
You are given a graph with nn nodes and mm directed edges. One lowercase letter is assigned to each node. We define a path's value as the number of the most frequently occurring letter. For example, if letters on a path are "abaca", then the value of that path is 33 . Your task is find a path whose value is the largest.
输入输出格式
输入格式:
The first line contains two positive integers n,mn,m ( 1<=n,m<=3000001<=n,m<=300000 ), denoting that the graph has nn nodes and mmdirected edges.
The second line contains a string ss with only lowercase English letters. The ii -th character is the letter assigned to the ii -th node.
Then mm lines follow. Each line contains two integers x,yx,y ( 1<=x,y<=n1<=x,y<=n ), describing a directed edge from xx to yy . Note that xx can be equal to yy and there can be multiple edges between xx and yy . Also the graph can be not connected.
输出格式:
Output a single line with a single integer denoting the largest value. If the value can be arbitrarily large, output -1 instead.
输入输出样例
5 4
abaca
1 2
1 3
3 4
4 5
3
6 6
xzyabc
1 2
3 1
2 3
5 4
4 3
6 4
-1
10 14
xzyzyzyzqx
1 2
2 4
3 5
4 5
2 6
6 8
6 5
2 10
3 9
10 9
4 6
1 10
2 8
3 7
4
说明
In the first sample, the path with largest value is 1→3→4→51→3→4→5 . The value is 33 because the letter 'a' appears 33 times.
XJB DP就行了,之前判一下是不是DAG
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#define ll long long
#define maxn 300005
using namespace std;
int ans=0,g[maxn];
int n,m,id[maxn],u,v;
int f[maxn],hd[maxn];
int to[maxn],ne[maxn];
bool vis[maxn];
char s[maxn]; inline int num(char x){
return x-'a';
} inline bool topsort(){
queue<int> q;
int x,tot=0; for(int i=1;i<=n;i++) if(!id[i]) q.push(i); while(!q.empty()){
x=q.front(),q.pop(),tot++;
for(int i=hd[x];i;i=ne[i]) if(!(--id[to[i]]))
q.push(to[i]);
} return tot==n;
} int dp(int x){
if(vis[x]) return g[x];
vis[x]=1,g[x]=0;
for(int i=hd[x];i;i=ne[i]) g[x]=max(g[x],dp(to[i]));
g[x]+=f[x];
return g[x];
} int main(){
scanf("%d%d",&n,&m);
scanf("%s",s+1);
for(int i=1;i<=m;i++){
scanf("%d%d",&u,&v);
to[i]=v,ne[i]=hd[u];
hd[u]=i,id[v]++;
} if(!topsort()){
puts("-1");
return 0;
} for(int i=0;i<26;i++){
memset(vis,0,sizeof(vis));
for(int j=1;j<=n;j++) f[j]=(num(s[j])==i); for(int j=1;j<=n;j++) ans=max(ans,dp(j));
} printf("%d\n",ans);
return 0; }
Codeforces 919 D Substring的更多相关文章
- [Codeforces 873B]Balanced Substring
Description You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s ...
- Codeforces 1015F Bracket Substring AC自动机 + dp
Bracket Substring 这么垃圾的题怎么以前都不会写啊, 现在一眼怎么就会啊.... 考虑dp[ i ][ j ][ k ][ op ] 表示 已经填了 i 个空格, 末尾串匹配到 所给串 ...
- CodeForces - 873B Balanced Substring(思维)
inputstandard input outputstandard output You are given a string s consisting only of characters 0 a ...
- CF 919 D. Substring
D. Substring 链接 题意: 在一张有向图中,定义路径的权值为路径中出现次数最多的字符出现的次数,求一条权值最大的路径.如果权值可以无限大,输出-1. 分析: 注意是一张有向图.如果存在环那 ...
- Codeforces 873B - Balanced Substring(思维)
题目链接:http://codeforces.com/problemset/problem/873/B 题目大意:一个字符串全部由‘0’和‘1’组成,当一段区间[l,r]内的‘0’和‘1’个数相等,则 ...
- Codeforces 919D:Substring(拓扑排序+DP)
D. Substring time limit: per test3 seconds memory limit: per test256 megabytes inputstandard: input ...
- Codeforces 919 E Congruence Equation
题目描述 Given an integer xx . Your task is to find out how many positive integers nn ( 1<=n<=x1&l ...
- Codeforces 919 C. Seat Arrangements
C. Seat Arrangements time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces 919 B. Perfect Number
B. Perfect Number time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
随机推荐
- 15,scrapy中selenium的应用
引入 在通过scrapy框架进行某些网站数据爬取的时候,往往会碰到页面动态数据加载的情况发生如果直接用scrapy对其url发请求,是获取不到那部分动态加载出来的数据值,但是通过观察会发现,通过浏览器 ...
- 关于spark入门报错 java.io.FileNotFoundException: File file:/home/dummy/spark_log/file1.txt does not exist
不想看废话的可以直接拉到最底看总结 废话开始: master: master主机存在文件,却报 执行spark-shell语句: ./spark-shell --master spark://ma ...
- SpringMVC基本概念
DispatcherServlet:MVC的前端控制器,浏览器用户的请求经过DispatcherServlet的分发,到达合适的controller,生产业务数据所需要的model,model通过Di ...
- day02_02.能被3整除的个位数为6的数
第2题 能被3整除的个位数为6的数 难度增加一点点,再接再厉 注意: 把一些限制条件,用PHP编程的语言来执行 题目:输出100以内(不含100)能被3整除且个位数为6的所有整数 <?php f ...
- python 删除重复文件 附源代码
啥也不说了,直接上源码 #! /usr/bin/env python #coding=utf-8 import os import md5 import time def getmd5( filena ...
- Notadd 2.0 全新 Node.js 版本~ (开发中) [从 PHP 到 node 的踩坑记]
对于 Notadd 我们本来期望它实现更多... 尽管我们也尝试做了很多努力,但是由于 PHP 本身的局限,以及考虑到开发环境配置的复杂程度,最终使用了折中方案.接下来,我们谈谈整个技术选型历程,也供 ...
- LeetCode with Python -> Dynamic Programming
198. House Robber You are a professional robber planning to rob houses along a street. Each house ha ...
- ACM-ICPC 2018 焦作赛区网络预赛
这场打得还是比较爽的,但是队友差一点就再过一题,还是难受啊. 每天都有新的难过 A. Magic Mirror Jessie has a magic mirror. Every morning she ...
- linux批量匹配移动文件的方法
需求 有需要移动的文件名清单 filename.txt filename.txt 内容如下: 15542842 1582457 1282427 1532158 4542457 1582453 6552 ...
- selenium webdriver——鼠标事件
Web产品中提供了丰富的鼠标交互方式,例如鼠标右击.双击.悬停.甚至是鼠标拖动等功能,在WebDriver中,将这些关于鼠标操作的方法 封装在ActionChains类中: ActionChains类 ...