Alice likes word "nineteen" very much. She has a string s and wants the string to contain as many such words as possible. For that reason she can rearrange the letters of the string.
For example, if she has string "xiineteenppnnnewtnee", she can get string "xnineteenppnineteenw", containing (the occurrences marked) two such words. More formally, word "nineteen" occurs in the string the number of times you can read it starting from some letter of the string. Of course, you shouldn't skip letters.
Help her to find the maximum number of "nineteen"s that she can get in her string.
The first line contains a non-empty string s, consisting only of lowercase English letters. The length of string s doesn't exceed 100.
Print a single integer — the maximum number of "nineteen"s that she can get in her string.
nniinneetteeeenn
2
nneteenabcnneteenabcnneteenabcnneteenabcnneteenabcii
2
nineteenineteen
2
这是昨天CF比赛中的A题,因为考虑不周,提交后被别人Hack了。今天看了下别人的代码,才找出昨天的漏洞。
题意:给出一个长度不超过100的字符串,可以任意改变字符串中元素的位置,问最多可以找出来多少个“nineteen”。分析:一个nineteen中包含3个n,1个i,3个e,1个t,所以可以根据这4个字母在字符串中出现的次数来确定包含多少个"nineteen"。如果字符串中至少包含2个"nineteen",则前一个"nineteen"中的最后一个'n'可以当做后一个"nineteen"中的第一个'n',即2个"nineteen"可以公用一个'n'。这样除了第一个"nineteen"需要3个'n',后面的只需2个'n'即可组成一个"nineteen"。而其他字母不能和别的"nineteen"公用。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
char s[111];
int a[5];
while(~scanf("%s",s))
{
memset(a,0,sizeof(a));
int len = strlen(s);
for(int i = 0; i < len; i++)
{
if(s[i] == 'n')
a[0]++;
else if(s[i] == 'i')
a[1]++;
else if(s[i] == 'e')
a[2]++;
else if(s[i] == 't')
a[3]++;
}
a[0] = (a[0] - 1) / 2; //根据n的数量算出最多包含"nineteen"的数量
a[2] /= 3;
sort(a,a+4);
printf("%d\n",a[0]);
}
return 0;
}