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. 21-Add Two Numbers-Leetcode

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  2. Phoenix二级索引

    Phoenix Hbase适合存储大量的对关系运算要求低的NOSQL数据,受Hbase 设计上的限制不能直接使用原生的API执行在关系数据库中普遍使用的条件判断和聚合等操作.Hbase很优秀,一些团队 ...

  3. 答应我,这次必须搞懂!痛点难点Promise。(小点心async/await,基于Promise的更优方案)

    Promise 出现的原因 在 Promise 出现以前,我们处理一个异步网络请求,大概是这样: // 请求 代表 一个异步网络调用. // 请求结果 代表网络请求的响应. 请求1(function( ...

  4. Apache2配置文件解读

    每次碰到都不知道具体的作用,所以来分析一下 配置文件结构 apache2在启动的时候自动读取/etc/apache2/apache2.conf文件的配置信息,不同的配置项按功能分布在不同的文件中,然后 ...

  5. JAVA中的六种日期类型使用

    基本的6种日期类 /** * 六种时间类型的类 * 数据库格式的时间三种格式 */ java.util.Date date = new java.util.Date();//年与日时分秒 //数据库的 ...

  6. 如何将List集合中相同属性的对象合并

    在实际的业务处理中,我们经常会碰到需要合并同一个集合内相同属性对象的情况,比如,同一个用户短时间内下的订单,我们需要将各个订单的金额合并成一个总金额.那么用lambda表达式和HashMap怎么分别处 ...

  7. Windows zip版本安装MySQL

    Windows --MySQL zip版本安装记录: step1. 官网download zip包:http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5. ...

  8. JmxTest

    package mbeanTest; import java.util.Set; import javax.management.Attribute; import javax.management. ...

  9. Output of C++ Program | Set 10

    Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 #include<st ...

  10. java Map集合类

    ---恢复内容开始--- Map提供了一个更通用的元素存储方法,Map集合类用于存储元素对(称作"键"和"值"),其中每个键映射到一个值. 了解Map接口和方法 ...