题目描述:
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
andmelee
are unbalanced, while neithernoon
nora
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:
ssOutput
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
neededSample Output 1 Copy
Copy
2 5The string s2s3s4s5s2s3s4s5 ==
eede
is unbalanced. There are also other unbalanced substrings. For example, the output2 6
will also be accepted.Sample Input 2 Copy
Copy
atcoderSample Output 2 Copy
Copy
-1 -1The string
atcoder
contains no unbalanced substring.
题目大意:
在原串中找一个字串,该字串中某一个字符的个数占该字串长度一半以上。
思路:
要占据一半以上,要么相邻的两个一样,最坏情况下也得每隔一位出现一次。
所以直接考虑2位或者3位的字串即可,
开始没想到这个思路,只会暴力。。。
代码实现:
#include<bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
#define io ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
const int N=2e5+100;
using namespace std;
int main()
{
io;
string str;
while(cin>>str)
{
int len=str.length();
int f=0;
for(int i=0;i<=len-1;i++)
{
if(str[i]==str[i+1]&&i+1<len)
{
f=1;
cout<<i+1<<" "<<i+2<<endl;
break;
}
else if(str[i]==str[i+2]&&i+2<len)
{
cout<<i+1<<" "<<i+3<<endl;
f=1;
break;
}
}
if(f==0)cout<<-1<<" "<<-1<<endl;
}
return 0;
}
The end;