AtCoder Regular Contest 059--D - アンバランス / Unbalanced(规律)

博客围绕判断字符串是否存在不平衡子串展开,不平衡子串指长度至少为2且有字符出现次数超一半的子串。给出问题描述、约束条件、部分得分规则等,还提及题解思路,指出只有AA或AXA两种形式。

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

D - アンバランス / Unbalanced


Time Limit: 2 sec / Memory Limit: 256 MB

Score : 400400 points

Problem Statement

Given a string tt, we will call it unbalanced if and only if the length of tt is at least 22, and more than half of the letters in tt are the same. For example, both voodoo and melee are unbalanced, while neither noon nor a is.

You are given a string ss consisting of lowercase letters. Determine if there exists a (contiguous) substring of ss that is unbalanced. If the answer is positive, show a position where such a substring occurs in ss.

Constraints

  • 2≦|s|≦1052≦|s|≦105
  • ss consists of lowercase letters.

Partial Score

  • 200200 points will be awarded for passing the test set satisfying 2≦N≦1002≦N≦100.

Input

The input is given from Standard Input in the following format:

ss

Output

If there exists no unbalanced substring of ss, print -1 -1.

If there exists an unbalanced substring of ss, let one such substring be sasa+1...sbsasa+1...sb (1≦a<b≦|s|)(1≦a<b≦|s|), and print \(a\) \(b\). If there exists more than one such substring, any of them will be accepted.


Sample Input 1 Copy

Copy

needed

Sample Output 1 Copy

Copy

2 5

The string s2s3s4s5s2s3s4s5 == eede is unbalanced. There are also other unbalanced substrings. For example, the output 2 6 will also be accepted.


Sample Input 2 Copy

Copy

atcoder

Sample Output 2 Copy

Copy

-1 -1

The string atcoder contains no unbalanced substring.

问是否存在子串使得长度>1且其中有字符出现的次数大于长度的一半。

下面的题解中注释的是错误思想,因为这样枚举不到所有区间。想错了。其实只有两种情况,就是AA或AXA这样的形式。

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define S(x) scanf("%d",&x);
#define SS(x,y) scanf("%d%d",&x,&y);
char num[100006];
int vis[100006];
struct node
{
    int id;
    int l,r;
    int cou;
    node()
    {
        cou=0;
        l=0;
        r=0;
    }
} a[100006];
int main()
{
    scanf("%s",num);
    int len=strlen(num);
    for(int i=0;i<len;i++)
    {
        if(num[i]==num[i+2])
        {
            printf("%d %d",i+1,i+3);
            return 0;
        }
        if(num[i]==num[i+1])
        {
            printf("%d %d",i+1,i+2);
            return 0;
        }
    }
    printf("-1 -1");
    /*for(int i=0; i<len; i++)
    {
        vis[num[i]-'a']++;
        if(vis[num[i]-'a']==1)
        {
            a[num[i]-'a'].l=i;
            a[num[i]-'a'].cou=1;
        }
        else if(vis[num[i]-'a']>1)
        {
            a[num[i]-'a'].r=i;
            a[num[i]-'a'].cou=vis[num[i]-'a'];//---
        }
        for(int j=0; j<28; j++)
        {
            if((a[j].r-a[j].l+1)/2<a[j].cou&&((a[j].r-a[j].l+1)>=2))
            {
                printf("%d %d",a[j].l+1,a[j].r+1);
                return 0;
            }
        }
    }
    //枚举26个字母
    printf("-1 -1");//---//*/
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值