连续出现的字符

GitHub跳转原题关系图返回列表

题意与原解析均从本地 OpenJudge 缓存迁移。

OJ: noi_openjudge

题目 ID: ch0109-11

难度:未知

标签:字符串连续段python

日期: 2026-07-30 23:01

题意

完整题面见同目录的 problem.md

思路

线性维护当前连续段长度,首次达到 kk 时输出该字符。

代码

Python代码

python
required = int(input())
text = input().strip()
count = 1
answer = "No"
for index in range(1, len(text)):
    count = count + 1 if text[index] == text[index - 1] else 1
    if count >= required:
        answer = text[index]
        break
if required == 1:
    answer = text[0]
print(answer)

C++代码

cpp
#include <cstdio>
#include <cstring>

int n;
char str[2000];
int main(){
    scanf("%d",&n);
    scanf("%s",str+1);
    int len = strlen(str+1);
    int i,cnt=1;
    for (i=2;i<=len;i++){
        if( str[i] == str[i-1]){
            cnt++;
        }
        else {
            cnt = 1;
        }
        if( cnt >=n){
            printf("%c",str[i]);
            return 0;
        }
    }
    printf("No");
    return 0;
}

复杂度

总结