某日,一只蚂蚱来到了一片字母草地。草地由n个大写字母组成。蚂蚱一开始在草地的最左边(位置0),它要去往草地的最右边(位置n+1)。这只蚂蚱只在元音字母上跳跃(起点和终点除外)。定义这只蚂蚱的跳跃能力为它一次跳跃能跳过的最远距离。
试问,若要让这只蚂蚱顺利到达终点,蚂蚱的跳跃能力最小是多少?下图是样例1的参考图:
元音字母有: 'A', 'E', 'I', 'O', 'U','Y'.
Input
一行一个字符串s,只有大写字母,描述这个草地。长度小于 1000。
Output
输出一个整数,表示这只蚂蚱最小跳跃能力。
Sample Input
ABABBBACFEYUKOTT
AAA
Sample Output
4
1
此题状态比较多,要注意
1、只有一个字母的情况
2、字符串中没有原元音的情况
3、最后一个元音到末尾距离<最大元音之间的距离的情况
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cmath>
#include<set>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
const double pi = 3.1415926;
const int maxn = 1e5 + 10;
string s;
int ans = 0;
int cnt;
int main()
{
ios::sync_with_stdio(false);
cin >> s;
int n = s.size();
if (n == 1&&(s[0]=='A'|| s[0] == 'E' || s[0] == 'I' || s[0] == 'O' || s[0] == 'U' || s[0] == 'Y' )) {
cout << 1 << endl;
return 0;
}
else if(n==1)
{
cout << 2 << endl;
return 0;
}
int j = -1;
for (int i = 0; i < n; i++) {
if (s[i] == 'A' || s[i] == 'E' || s[i] == 'I' || s[i] == 'O' || s[i] == 'U' || s[i] == 'Y')
{
cnt = i;
ans = max(ans, i - j);
j = i;
}
}
if (ans == 0)
cout << n + 1 << endl;
else if (ans < n - cnt)
cout << n-cnt << endl;
else
cout << ans << endl;
return 0;
}