D. Substring
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的更多相关文章
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
- 最长回文子串-LeetCode 5 Longest Palindromic Substring
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- POJ3693 Maximum repetition substring [后缀数组 ST表]
Maximum repetition substring Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9458 Acc ...
- [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 ...
- substring的用法
public String substring(int beginIndex, int endIndex) 返回一个新字符串,它是此字符串的一个子字符串.该子字符串从指定的 beginIndex 处开 ...
- jQuery之常用且重要方法梳理(target,arguments,slice,substring,data,trigger,Attr)-(一)
1.jquery data(name) data() 方法向被选元素附加数据,或者从被选元素获取数据. $("#btn1").click(function(){ $(" ...
- leetcode--5. Longest Palindromic Substring
题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...
- C#和Java中的Substring()
吐槽-使用清理软件整理电脑要注意,不要清理的"太狠",不然你会受伤的! C#中的Substring() 示例 实现代码 using System;using System.Coll ...
- JavaScript中的slice,splice,substr,substring,split的区别
万恶的输入法,在sublime中会显示出繁体字,各位看官见谅. 1.slice()方法:该方法在数组和string对象中都拥有. var a = [1,2,3,4,5,6]; var s = 'thi ...
- sql server中substring的用法
SQL 中的 substring 函数是用来截取一个栏位资料中的其中一部分. 例如,我们需要将字符串'abdcsef'中的'abd'给提取出来,则可用substring 来实现: ,) 结果: 'ab ...
随机推荐
- 使用SpringBoot实现文件的上传
使用SpringBoot实现文件的上传 springboot可以直接使用 org.springframework.web.multipart.MultipartFile 所以非常容易实现 一.首先是简 ...
- InnoDB学习(一)之BufferPool
我们知道InnoDB数据库的数据是持久化在磁盘上的,而磁盘的IO速度很慢,如果每次数据库访问都直接访问磁盘,显然严重影响数据库的性能.为了提升数据库的访问性能,InnoDB为数据库的数据增加了内存缓存 ...
- Elasticsearch中关于transform的一个问题?
背景:现在有一个业务,派件业务,业务员今天去派件(扫描产生一条派件记录),派件可能会有重复派件的情况,第二天再派送(记录被更新,以最新的派件操作为准).现在需要分业务员按天统计每天的派件数量.es版本 ...
- Yarn 公平调度器案例
目录 公平调度器案例 需求 配置多队列的公平调度器 1 修改yarn-site.xml文件,加入以下从参数 2 配置fair-scheduler.xml 3 分发配置文件重启yarn 4 测试提交任务 ...
- 关于redis HSCAN count参数不生效的问题
这的确是个坑,HSCAN是为了处理大量数据而设计的,可能也是因为这个原因,在数据量较少的情况下count参数并不会生效,具体阈值是多少并没有实际测验过不过可以断定的是一百条数据一下估计是不会生效的.
- day25 组合和内置函数
day25 组合和内置函数 一.组合 # 解决类与类之间代码冗余问题: 1. 继承 2. 组合 组合:一个对象拥有一个属性, 属性的值必须是另外一个对象 继承满足的是:什么是什么的关系 # is-a ...
- 【编程思想】【设计模式】【行为模式Behavioral】访问者模式Visitor
Python版 https://github.com/faif/python-patterns/blob/master/behavioral/visitor.py #!/usr/bin/env pyt ...
- SpringAOP浅析
1.问题 问题:想要添加日志记录.性能监控.安全监测 2.最初解决方案 2.1.最初解决方案 缺点:太多重复代码,且紧耦合 2.2.抽象类进行共性设计,子类进行个性设计,此处不讲解,缺点一荣俱荣,一损 ...
- Linux上用Jexus部署Asp.Net网站:常规部署与Docker部署
(一)常规部署 一.把 jexus压缩包下载到linux临时文件夹中. cd /tmp wget linuxdot.net/down/jexus-6.2.x-arm64.tar.gz (不同的操作系统 ...
- MFC入门示例之组合框(CComboBox)、列表框(CListBox)
1 //添加按钮点击事件 2 void CMFCApplication4Dlg::OnBnClickedButton1() 3 { 4 CString strText; 5 //获取文本框的值 6 G ...