AtCoder Beginner Contest 043 D - アンバランス / Unbalanced
题目链接:http://abc043.contest.atcoder.jp/tasks/arc059_b
Time limit : 2sec / Memory limit : 256MB
Score : 400 points
Problem Statement
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 neither noon
nor a
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.
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.
题意:给定一个序列,如果某个子序列中的有一个字母的数量大于子序列长度的一半则为平衡序列。
题解:其实本题只要判断了相邻两个字母是否一样或者间隔一个的字母是否相同就能判断出是否有子序列满足平衡。在往后判断只是重复了上述的情况
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <cstdio> 5 #include <vector> 6 #include <cstdlib> 7 #include <iomanip> 8 #include <cmath> 9 #include <ctime> 10 #include <map> 11 #include <set> 12 #include <queue> 13 #include <stack> 14 using namespace std; 15 #define lowbit(x) (x&(-x)) 16 #define max(x,y) (x>y?x:y) 17 #define min(x,y) (x<y?x:y) 18 #define MAX 100000000000000000 19 #define MOD 1000000007 20 #define pi acos(-1.0) 21 #define ei exp(1) 22 #define PI 3.141592653589793238462 23 #define INF 0x3f3f3f3f3f 24 #define mem(a) (memset(a,0,sizeof(a))) 25 typedef long long ll; 26 ll gcd(ll a,ll b){ 27 return b?gcd(b,a%b):a; 28 } 29 bool cmp(int x,int y) 30 { 31 return x>y; 32 } 33 const int N=200005; 34 const int mod=1e9+7; 35 int main() 36 { 37 std::ios::sync_with_stdio(false); 38 cin.tie(0); 39 int flag=0; 40 string s; 41 cin>>s; 42 for(int i=0;i<s.size()-1;i++){ 43 if(s[i]==s[i+1]){ 44 cout<<i+1<<" "<<i+2<<endl; 45 flag=1; 46 break; 47 } 48 else if(i<s.size()-2&&s[i]==s[i+2]){ 49 cout<<i+1<<" "<<i+3<<endl; 50 flag=1; 51 break; 52 } 53 } 54 if(!flag) 55 cout<<-1<<" "<<-1<<endl; 56 return 0; 57 }