(个人水平有限,请见谅!)
题目描述:
有一条彩色宝石项链,是由很多种不同的宝石组成的,包括红宝石,蓝宝石,钻石,翡翠,珍珠等。有一天国王把项链赏赐给了一个学者,并跟他说,你可以带走这条项链,但是王后很喜欢红宝石,蓝宝石,紫水晶,翡翠和钻石这五种,我要你从项链中截取连续的一小段还给我,这一段中必须包含所有的这五种宝石,剩下的部分你可以带走。如果无法找到则一个也无法带走。请帮助学者找出如何切分项链才能够拿到最多的宝石。
输入描述:
我们用每种字符代表一种宝石,A表示红宝石,B表示蓝宝石,C代表紫水晶,D代表翡翠,E代表钻石,F代表玉石,G代表玻璃等等,我们用一个全部为大写字母的字符序列表示项链的宝石序列,注意项链是首尾相接的。每行代表一种情况。
输出描述:
输出学者能够拿到的最多的宝石数量。每行一个。
输入:
ABCYDYE
ATTMBQECPD
输出:
1
3
代码示例:
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
int main()
{
string str;
string gem_five = "ABCDE", model = "ABCDE", temp = "";
int len_min = 10000, len, end;
vector <int> pos;
cin >> str;
str += str;
for (int i = 0; i < str.size(); i++)
{
if (gem_five.size() > 0)
{
for (int j = 0; j < gem_five.size(); j++)
{
if (str[i] == gem_five[j])
{
temp += str[i];
gem_five.erase(j,1);
pos.push_back(i);
}
}
if (gem_five.size() == 0)
{
len = pos[4] - pos[0];
len_min = min(len_min, len);
}
}
else
{
for (int j = 0; j < 5; j++)
{
if (str[i] == temp[j])
{
temp += temp[j];
temp.erase(j,1);
pos.push_back(i);
pos.erase(pos.begin()+j);
len = pos[4] - pos[0];
len_min = min(len_min, len);
}
}
}
}
(pos.size() != 5) ? (cout << 0):(cout << str.size()/2-len_min-1);
}