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;
}