D. Substring

题意:

给你一个有向图,然后给你一串字符串第i个点的值为第i个字符,然后给你m条有向边,从中找一条路径然后这条路径中点的值相同的个数的最大值,如果图有环输出-1。

思路:

拓扑排序+dp

我们需要找到一条路径的开头,用拓扑排序即可,然后每个点维护26个字母在到当前点的路径上出现最大值即可,复杂度O(n*26)。

题链

代码:

#include<bits/stdc++.h>
using namespace std;
int dp[300005][26];
class Solution
{
public:
int n,m;
char str[300005];
int cnt[300005];
vector<int>vec[300005];
queue<int>que;
map<pair<int,int>,int>my;
Solution()
{
memset(str,0,sizeof(str));
memset(cnt,0,sizeof(cnt));
for(int i = 0; i < 300005; i++)
vec[i].clear();
while(!que.empty())
{
que.pop();
}
}
void in_put()
{
scanf("%d %d",&n,&m);
scanf("%s",str);
for(int i = 0; i < m; i++)
{
int x,y;
scanf("%d %d",&x,&y);
if(!my.count(make_pair(x,y)))
{
my[make_pair(x,y)] = 1;
cnt[y]++;
vec[x].push_back(y);
}
}
}
int TUPU()
{
int maxx = -1;
int cn = 0;
for(int i = 1; i <= n; i++)
{
if(cnt[i] == 0)
{
que.push(i);
dp[i][str[i-1] - 'a'] = 1;
maxx = 1;
cn++;
}
}
while(!que.empty())
{
int id = que.front();
que.pop();
for(int i = 0; i < vec[id].size(); i++)
{
int ic = vec[id][i];
for(int j = 0; j < 26; j++)
{
if(j == str[ic - 1] - 'a')
dp[ic][j] = max(dp[ic][j],dp[id][j] + 1);
else dp[ic][j] = max(dp[ic][j],dp[id][j]);
// printf("%d %d %d\n",id,maxx,ic);
maxx = max(dp[ic][j],maxx);
}
cnt[ic]--;
if(cnt[ic] == 0)
cn++,que.push(ic);
}
}
if(cn == n)
return maxx;
return -1;
}
};
int main(void)
{
Solution A;
A.in_put();
cout<<A.TUPU();
return 0;
}

D. Substring的更多相关文章

  1. LeetCode[3] Longest Substring Without Repeating Characters

    题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...

  2. 最长回文子串-LeetCode 5 Longest Palindromic Substring

    题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  3. POJ3693 Maximum repetition substring [后缀数组 ST表]

    Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9458   Acc ...

  4. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  5. substring的用法

    public String substring(int beginIndex, int endIndex) 返回一个新字符串,它是此字符串的一个子字符串.该子字符串从指定的 beginIndex 处开 ...

  6. jQuery之常用且重要方法梳理(target,arguments,slice,substring,data,trigger,Attr)-(一)

    1.jquery  data(name) data() 方法向被选元素附加数据,或者从被选元素获取数据. $("#btn1").click(function(){ $(" ...

  7. leetcode--5. Longest Palindromic Substring

    题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...

  8. C#和Java中的Substring()

    吐槽-使用清理软件整理电脑要注意,不要清理的"太狠",不然你会受伤的! C#中的Substring() 示例 实现代码 using System;using System.Coll ...

  9. JavaScript中的slice,splice,substr,substring,split的区别

    万恶的输入法,在sublime中会显示出繁体字,各位看官见谅. 1.slice()方法:该方法在数组和string对象中都拥有. var a = [1,2,3,4,5,6]; var s = 'thi ...

  10. sql server中substring的用法

    SQL 中的 substring 函数是用来截取一个栏位资料中的其中一部分. 例如,我们需要将字符串'abdcsef'中的'abd'给提取出来,则可用substring 来实现: ,) 结果: 'ab ...

随机推荐

  1. Linux服务器I/O性能分析-2

    一.如何正确分析IO性能 1.1 BLKTRACE分析IO性能 之前的文章已经说明,要是系统发生I/O性能问题,我们常用的命令是无法精确定位问题(内核I/O调度器消耗的时间和硬件消耗的时间,这个不能作 ...

  2. mysql—Linux系统直接进入mysql服务器,并实现一些基础操作

    首先,我们需要通过以下命令来检查MySQL服务器是否启动: ps -ef | grep mysqld 如果MySql已经启动,以上命令将输出mysql进程列表 如果mysql未启动,你可以使用以下命令 ...

  3. Oracle-distinct()用法、count(distinct( 字段A || 字段B))是什么意思?distinct多个字段

    0.distinct用法 在oracle中distinct的使用主要是在查询中去除重复出现的数据 直接在字段前加distinct关键字即可,如:select distinct 名字 from tabl ...

  4. ubuntu安装配置ssh-connect to host localhost port 22: Connection refused

    在安装ssh,经常出现 ssh: connect to host localhost port 22: Connection refused 从以下几点去检查: 1.是否安装ssh-server: 打 ...

  5. 33、搜索旋转排序数组 | 算法(leetode,附思维导图 + 全部解法)300题

    零 标题:算法(leetode,附思维导图 + 全部解法)300题之(33)搜索旋转排序数组 一 题目描述! 题目描述 二 解法总览(思维导图) 三 全部解法 1 方案1 1)代码: // 方案1 & ...

  6. Git(一)【基本使用,集成IDEA,GitHub】

    目录 一.本地库操作 ①基本操作 1.初始化本地库 2.设置用户签名|用户名|邮箱 3.查看本地库状态 4.添加暂存区 5.提交到本地库 6.查看文件modify详情 ②历史版本以及回退 1.查看历史 ...

  7. Swagger2异常 java.lang.NumberFormatException: For input string: ""

    问题在访问swagger首页时报错: java.lang.NumberFormatException: For input string: "" at java.lang.Numb ...

  8. redis 之 哨兵

    #:编译安装redis4.0 [root@master ~]# tar xf redis-4.0.14.tar.gz [root@master ~]# cd redis-4.0.14/ [root@m ...

  9. 使用buffered流结合byte数组,读入文件中的内容,包括中文字符

    package com.itcast.demo05.Buffered;import java.io.BufferedInputStream;import java.io.FileInputStream ...

  10. 关于requests.exceptions.ConnectionError: HTTPSConnectionPool的问题

    错误如下: raise ConnectionError(e, request=request)requests.exceptions.ConnectionError: HTTPSConnectionP ...