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. EXCEL-表格安全性:加密给与不同操作权限、表格怎么不让别人复制粘贴?

    1.下方表格名称右键-保护工作表,将所有选项取消勾选 然后设置一个密码就可以了 2.文件旁的三条线点开-选项-安全性,设置编辑权限密码,确定

  2. Hive-删除表(drop、truncate的区别)

    Hive删除操作主要分为几大类:删除数据(保留表).删除库表.删除分区.我将以下图为例清空iot_devicelocation中的数据,之后再删除表.库等. 解释: use xpu123;   #使用 ...

  3. UE4 C++工程以Game模式启动

    UE4版本:4.24.3源码编译版本 Windows10 + VS2019环境 UE4 C++工程,默认情况下VS中直接运行是启动Editor模式: 有时为了调试等目的需要以Game模式启动,可以避免 ...

  4. 如何利用官方SDK文件来辅助开发

    如何利用官方SDK文件来辅助开发 1.首先要先知道什么是SDK? SDK或者SDK包指的是,半导体厂商针对自己研发的芯片,同步推出的一个软件开发工具包. 它可以简单的为某个程序设计语言提供应用程序接口 ...

  5. 强化学习实战 | 表格型Q-Learning玩井字棋(二)

    在 强化学习实战 | 表格型Q-Learning玩井字棋(一)中,我们构建了以Game() 和 Agent() 类为基础的框架,本篇我们要让agent不断对弈,维护Q表格,提升棋力.那么我们先来盘算一 ...

  6. Linux学习 - 文件系统属性chattr权限

    change file attributes on 啊linux file system 1 功能 可以防止误操作 2 chattr命令格式 chattr [+-=] [选项] 文件或目录名 + 增加 ...

  7. 【Linux】【Services】【SaaS】Docker+kubernetes(10. 利用反向代理实现服务高可用)

    1. 简介 1.1. 由于K8S并没有自己的集群,所以需要借助其他软件来实现,公司的生产环境使用的是Nginx,想要支持TCP转发要额外安装模块,测试环境中我就使用HAPROXY了 1.2. 由于是做 ...

  8. 拷贝txt文本中的某行的数据到excel中

    package com.hope.day01;import java.io.*;import java.util.ArrayList;public class HelloWorld {    publ ...

  9. 创建线程的第二种方式------实现Runnable接口的方式

    package cn.itcast.demo16.Demo07.Runnable;/** * @author newcityman * @date 2019/7/22 - 23:17 */public ...

  10. linux 让.net 控制台后台运行

    命令     nohup 你的shell命令  & 例如    nohup dotnet  MQTTClient.dll & 输入完成后,终端会有提示 这时再按下回车 回到shell命 ...