题目
A. Nineteen
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
Input
The first line contains a non-empty string s, consisting only of lowercase English letters. The length of string s doesn’t exceed 100.
Output
Print a single integer — the maximum number of "nineteen"s that she can get in her string.
Examples
input
nniinneetteeeenn
output
2
input
nneteenabcnneteenabcnneteenabcnneteenabcnneteenabcii
output
2
input
nineteenineteen
output
2
题目理解
这道题目需要我们计算在所给的字符串中可以凑出多少个‘nineteen’。
当然,这道题目还是有一个小小的坑,如果不注意的话,就会一直wrong到怀疑人生。那就是当有多个nineteen的时候,前一个单词的最后一个字母n,可以当做下一个单词的开头,即可以是这样的一个存在:‘nineteenineteen’
解题思路
我们可以通过计算字符串中’n’、‘i’、‘e’、't’的个数,取其最小值来得到最终的结果
代码如下(python)
a = input().strip()
num_n = a.count('n')
num_e = a.count('e')
num_i = a.count('i')
num_t = a.count('t')
num_n = int((num_n -1) / 2)
num_e = int(num_e / 3)
print(min(num_n, num_e, num_i, num_t))
代码解释:
a = input().strip()
input()输入字符串,strip()的作用是删除字符串首尾指定字符(默认为空格和换行符)
num = a.count('')
a.count(b):统计a中b的个数
print(min(x))
min(x):输出x中的最小值