CF - div2 - 460 - D.Substring(拓扑排序 + DP)

本文介绍了一道关于有向图的问题,目标是找到一条路径,使得路径上出现最多的字母次数最大化。通过拓扑排序检查环的存在,并使用动态规划维护每条路径上的最大权值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Substring
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a graph with n nodes and m 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 3. Your task is find a path whose value is the largest.

Input
The first line contains two positive integers n, m (1 ≤ n, m ≤ 300 000), denoting that the graph has n nodes and m directed edges.

The second line contains a string s with only lowercase English letters. The i-th character is the letter assigned to the i-th node.

Then m lines follow. Each line contains two integers x, y (1 ≤ x, y ≤ n), describing a directed edge from x to y. Note that x can be equal to y and there can be multiple edges between x and y. Also the graph can be not connected.

Output
Output a single line with a single integer denoting the largest value. If the value can be arbitrarily large, output -1 instead.

Examples
inputCopy
5 4
abaca
1 2
1 3
3 4
4 5
output
3
inputCopy
6 6
xzyabc
1 2
3 1
2 3
5 4
4 3
6 4
output
-1
inputCopy
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
output
4
Note
In the first sample, the path with largest value is 1 → 3 → 4 → 5. The value is 3 because the letter ‘a’ appears 3 times.

题目链接http://codeforces.com/problemset/problem/919/D


题意:输入一个 nm。分别代表有 n 个点,m 条有向边。接下来输入一个长度为 n 的字符串 SSi 代表点 i 上的字符为 Si。最后输入 mx ,y,代表有一条边从 x 指向 y

:输出一条路径上的最大权值(一条路径上的权值等于在这条路径出现最多次字母的次数),若有环则输出 -1

数据范围1 ≤ n, m ≤ 300 0001 ≤ x, y ≤ n


解题思路:可以用拓扑排序验证是否有环,若进队列的点的个数 等于 总点个数,则说明该图没有环,反之则有环。接着我们可以在拓扑排序的过程维护答案。 dp[i][j] d p [ i ] [ j ] :代表到 点 i 时,字母 j 出现的最大次数。


代码如下

#include<bits/stdc++.h>
using namespace std;

const int maxn = 4e5 + 5;
vector<int> G[maxn];
int n,m,d[maxn],seq[maxn],dp[maxn][30];
char s[maxn];
int ma = 0;

void Init(){
    cin >> n >> m >> s + 1;
    for(int i = 1;i <= m;i++){
        int x,y;
        cin >> x >> y;
        d[y]++;
        G[x].push_back(y);
    }
}

void Slove(){
    int head = 1,tail = 0;
    for(int i = 1;i <= n;i++){
        if(d[i] == 0){
            seq[++tail] = i;
            dp[i][s[i] - 'a'] = 1;
        }
    }
    while(head <= tail){
        int u = seq[head];
        for(int i = 0;i < G[u].size();i++){
            int v = G[u][i];
            d[v]--;
            if(d[v] == 0){//不仅是在 d[v] = 0,才转移,而是每个状态都需要转移。
                seq[++tail] = v;    
            }
            for(int j = 0;j < 26;j++){
                if(s[v] - 'a' == j){
                    dp[v][j] = max(dp[v][j],dp[u][j] + 1); //dp的精髓是 取 max 或 min,要不然就是递推了。
                }
                else dp[v][j] = max(dp[v][j],dp[u][j]);
                ma = max(ma,dp[v][j]);
            }
        }
        head++;
    }
    if(tail != n) cout << "-1" << endl;//判断是否有环。
    else cout << ma << endl;
}

int main(){
    Init();
    Slove();
    return 0;
}
<think>好的,我现在要帮用户解释这段C#代码:strValue = strValue.Substring(1, strValue.Length - 1)。首先,我得确认自己对Substring方法的理解是否正确。Substring通常用于截取字符串的一部分,参数是起始索引和长度。记得C#中字符串的索引是从0开始的,所以第一个字符是索引0。 那这段代码中的参数是(1, strValue.Length - 1)。第一个参数是1,表示从索引1开始截取,也就是原字符串的第二个字符。第二个参数是长度,原字符串长度减去1。比如原字符串长度是5,减去1就是4,所以截取从索引1开始的4个字符。那原字符串如果是"Hello",截取后的结果应该是"ello"。这样看起来,这段代码的作用是去掉字符串的第一个字符。 接下来要考虑用户可能的疑问,比如为什么要这样写,有没有其他方法可以达到同样的效果。比如使用Substring(1)可能更简洁,因为如果不指定长度,默认会截取到字符串末尾。但用户给出的代码显式指定了长度,可能在某些情况下更安全,特别是当字符串长度可能为0的时候。不过如果原字符串长度为0,这里会抛出异常,所以用户需要注意处理空字符串的情况。 还需要考虑用户的应用场景。比如用户可能在处理某种格式的字符串,需要去除首字符,比如去掉括号、引号或者其他特定字符。或者是在处理文件路径、URL时去掉开头的斜杠。这时候需要提醒用户确保字符串至少有1个字符,否则会引发ArgumentOutOfRangeException。 另外,可能用户是从其他编程语言转过来的,比如JavaScript的substring参数是起始和结束索引,而C#的Substring是起始和长度,这点差异需要注意。如果用户混淆了这两个,可能会导致错误,所以需要强调参数的含义。 还要考虑有没有更高效的方法,比如使用Span或者指针,但对于大多数情况来说,Substring已经足够。不过需要注意的是,Substring会创建新的字符串对象,如果处理大量数据可能会有性能问题,这时候可以考虑其他方式。 总结一下,这段代码的作用是去除字符串的第一个字符,但需要注意原字符串的长度是否足够,避免异常。同时,可以建议更简洁的写法或者异常处理的注意事项,帮助用户更好地理解和应用这段代码。</think>这是C#中用于截取字符串的代码片段。其作用是从字符串中删除第一个字符,执行步骤如下: $$strValue.Length - 1$$ 表示原字符串长度减1 $$Substring(1, strValue.Length - 1)$$ 参数解读: 1. 起始位置:索引1(即第二个字符) 2. 截取长度:原字符串长度减1 示例演示: ```csharp string strValue = "Hello"; strValue = strValue.Substring(1, strValue.Length - 1); Console.WriteLine(strValue); // 输出"ello" ``` 注意事项: 1. 当原始字符串为空(长度为0)时,会抛出`ArgumentOutOfRangeException` 2. 等价写法:`strValue = strValue.Substring(1);`(当不指定长度时默认截取到末尾) 3. 实际效果等同于移除字符串的第一个字符 典型应用场景: - 处理带前缀的字符串(如$符号金额:"$100" → "100") - 去除特殊标记(如星号标记:"*重要通知" → "重要通知") - 清理格式字符(如引号包裹的字符串:`"\"text\""` → `"text"`)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值