One day, the Grasshopper was jumping on the lawn and found a piece of paper with a string. Grasshopper became interested what is the minimum jump ability he should have in order to be able to reach the far end of the string, jumping only on vowels of the English alphabet. Jump ability is the maximum possible length of his jump.
Formally, consider that at the begginning the Grasshopper is located directly in front of the leftmost character of the string. His goal is to reach the position right after the rightmost character of the string. In one jump the Grasshopper could jump to the right any distance from 1to the value of his jump ability.
The following letters are vowels: 'A', 'E', 'I', 'O', 'U' and 'Y'.
The first line contains non-empty string consisting of capital English letters. It is guaranteed that the length of the string does not exceed 100.
Print single integer a — the minimum jump ability of the Grasshopper (in the number of symbols) that is needed to overcome the given string, jumping only on vowels.
ABABBBACFEYUKOTT
4
AAA
1
题意就是一只蟋蟀从字符串起点开始跳跃,只能跳到元音上面,问蟋蟀最少需要跳多远,其实就是求最大间距,注意分类讨论不存在元音的情况:
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
typedef long long ll;
#define MAXN 1009
char c[MAXN];
int arr[MAXN];
int main()
{
cin>>c;
int l=strlen(c);
int k=0;
int flag=0;
for(int i=0;i<l;i++)
{
if(c[i]=='A'||c[i]=='E'||c[i]=='I'||c[i]=='O'||c[i]=='U'||c[i]=='Y')
{
flag=1;
arr[k++]=i;//记录下标
}
}
if(!flag)//不存在原因
{
cout<<l+1<<endl;
}
else
{
int s1=arr[0]+1;//左端极端
int s2=l-arr[k-1];//右端极端
int maxx=0;
for(int i=0;i<k;i++)
{
int l=arr[i+1]-arr[i];
if(l>maxx) maxx=l;中间部分
}
cout<<max(max(maxx,s1),s2)<<endl;//取最大值
}
return 0;
}