Problem Description
Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both
voodoo
andmelee
are unbalanced, while neithernoon
nora
is.You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.
Constraints
2≦|s|≦105
s consists of lowercase letters.
Partial Score
200 points will be awarded for passing the test set satisfying 2≦N≦100.
Input
The input is given from Standard Input in the following format:
s
Output
If there exists no unbalanced substring of s, print -1 -1.
If there exists an unbalanced substring of s, let one such substring be sasa+1…sb (1≦a<b≦|s|), and print a b. If there exists more than one such substring, any of them will be accepted.
Example
Sample Input 1
needed
Sample Output 1
2 5
The string s2s3s4s5 = eede is unbalanced. There are also other unbalanced substrings. For example, the output 2 6 will also be accepted.Sample Input 2
atcoder
Sample Output 2
-1 -1
The string atcoder contains no unbalanced substring.
题意:给出一个字符串 t,当 t 的中的字母一半以上是相同的时候,称为不平衡字符串,要求判断字符串中是否存在不平衡的子串,如果存在,则输出任意一个子串的位置
思路:
由于如果存在不平衡子串只需要任意输出一个子串位置即可,那么直接考虑最坏的情况
在最坏的情况下,字母相邻两个就相同或者隔一个就相同的话一定存在不平衡子串,因为如果隔两个以上相同的话,那么绝对不可能是一半相同,因此直接暴力枚举字符,判断相邻的的字符与隔一个的字符是否相同即可
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 1000000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;
char str[N];
int main(){
scanf("%s",str+1);
int len=strlen(str+1);
bool flag=true;
for(int i=2;i<=len;i++){
if(str[i]==str[i-1]){
printf("%d %d\n",i-1,i);
flag=false;
break;
}
else if(i>2&&str[i]==str[i-2]){
printf("%d %d\n",i-2,i);
flag=false;
break;
}
}
if(flag)
printf("-1 -1\n");
return 0;
}