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.
题意:
给定一个字符串让你找其中的一个不平衡子串(不平衡子串是某一个字符出现的次数超过该子串长度的一半)
题解:
若存在的话一定存在xx或x_x的情况。否则一定不存在,可以试着举例试试。
代码:
#include<bits/stdc++.h>
using namespace std;
char st[100009];
int main()
{
scanf("%s",st+1);
int len=strlen(st+1);
for(int i=1;i<=len;i++)
{
if(i+1<=len&&st[i+1]==st[i])
{
cout<<i<<" "<<i+1<<endl;
return 0;
}
if(i+2<=len&&st[i+2]==st[i])
{
cout<<i<<" "<<i+2<<endl;
return 0;
}
}
cout<<"-1 -1"<<endl;
}