[R17B]最多连胜
双指针解决
// [R17B]最多连胜.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
/*
题目地址:
https://bs.daimayuan.top/p/98
题目名称:
[R17B]最多连胜
题目描述:
有 n 场比赛,已知每场比赛的结果,求其中最多连续胜了多少场。
输入格式:
第一行包含一个整数 n,表示比赛的数量。
第二行包含一个长度为 n 的仅由 W 和 L 这两种字符构成的字符串 S,分别表示每场比赛的结果,其中 W 表示胜,L 表示败。
输出格式:
输出一个整数,表示连胜的最多场数。
数据范围:
对于 50% 的数据,n≤1000。
对于 100% 的数据,1≤n≤10^6。数据保证 S 仅由 W 和 L 这两种字符构成。
样例输入:
14
WLWWWLWWLLWWWW
样例输出:
4
样例输入:
4
LLLL
样例输出:
0
*/
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main()
{
int n;
char str[1000010]; memset(str, 0, sizeof(str));
cin >> n >> str;
int l = 0; int r = 0; int ans = 0;
while (l < n && r < n) {
while (l < n && str[l] == 'L') l++;
r = l;
while (r < n && str[r] == 'W') r++;
if (l < n) {
ans = max(ans, r - l);
}
l = r;
}
cout << ans << endl;
return 0;
}
作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
浙公网安备 33010602011771号