有一条彩色宝石项链,是由很多种不同的宝石组成的,包括红宝石,蓝宝石,钻石,翡翠,珍珠等。有一天过往把项链赏赐给了一个学者,并跟他说,你可以带走这条项链,但是往后很喜欢红宝石,蓝宝石,钻石,翡翠,珍珠这五种,我要你从项链中截取连续的一小段,这一小段中必须包含着五种宝石,剩下的部分你可以带走,如果你找不到这五种宝石,则一个也无法带走。请帮助学者找出如何切粉项链能拿到更多宝石的方法。
输入描述:
输入一行,一个字符串s,字符串长度length<=100,s中每个字符都是大写字母
输出描述:
输出一个整数,即s中减去包含ABCDE的最短字符串后的剩余字符数
代码,总体思路就是暴力循环
#include<iostream>
#include <vector>
#include <utility>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <math.h>
using namespace std;
int main()
{
string inputstr;
cin>>inputstr;
int len = inputstr.size();
cout<<len<<endl;
int maxGet = 0;
for(int i = 0; i < len; i++)
{
if(inputstr[i]>='A'||inputstr[i]<='E'){
int flags[5] = {0};
int j = i;
int cnt = 0; // 选中的宝石序列中宝石的个数
int sumflag = 0; //序列中已经找到的特殊宝石树木
while(sumflag<5&&cnt<len) {
if (inputstr[j % len] >= 'A' && inputstr[j % len] <= 'E' && flags[inputstr[j % len] - 'A'] == 0) {
flags[inputstr[j % len] - 'A'] = 1;
++sumflag;
}
++cnt;
++j;
}
if(maxGet<len-cnt)
maxGet = len-cnt;
}
}
cout<<maxGet<<endl;
}