AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 应用
  • 更多
    • 题解
    • 分享
    • 商店
    • 问答
    • 吐槽
  • App
  • 登录/注册

Codeforces contest/1837/problem/B. B. Comparison String    原题链接    简单

作者: 作者的头像   啼莺修竹 ,  2023-05-26 00:53:53 ,  所有人可见 ,  阅读 35


4


3
codeforce每日一题链接

题目描述

给你一个字符串,每个字符不是$’<’$,就是$’>’$。有一个数组$a$,$s[i]$为$’<’$表示a[i] < a[i+1],$s[i]$为$’>’$表示$a[i]>a[i+1]$,请返回$a$的最小代价。数组的代价是数组中不同元素的数量。

样例

输入样例1
4
4
<<>>
4
>><<
5
>>>>>
7
<><><><
输出样例1
3
3
6
2

算法

(暴力枚举) $O(n)$

找出最大连续的’<’或者’>’的长度,答案就是最大长度加1。

C++ 代码

//  https://www.acwing.com/blog/content/34755/

#include<bits/stdc++.h>

#define endl '\n'
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define pd push_back

using namespace std;

typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef pair<double, double> PDD;

const int N = 1e5 + 10, M = N * 2, INF = 0x3f3f3f3f;

int n, m;

inline void solve()
{
    cin>>n;
    string str;
    cin>>str;
    int res = 0;
    for(int i=0;i<n;i++){
        int j = i;
        while(j<n && str[j]==str[i]) j++;
        res = max(res, j - i);
        i = j - 1;
    }

    cout<<res + 1<<endl;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int T;
    cin>>T;
    while(T--)
    {
        solve();
    }

    return 0;
}

0 评论

你确定删除吗?
1024
x

© 2018-2023 AcWing 版权所有  |  京ICP备17053197号-1
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息