Codeforces-393A Nineteen

题目:


A. Nineteen
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// oj.cpp : 定义控制台应用程序的入口点。
//

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <sstream>
#include <string>
#include <numeric>
#include <cstring>
#include <map>
#include <set>
#include <utility>
#include <map>
#include <list>
#include <queue>
#include <functional>
#include <bitset>
#include <stack>
#include <deque>

using namespace std;

int main()
{
string L;
cin >> L;
int n = 0, i = 0, e = 0, t = 0;
for (char c : L)
{
switch (c)
{
case 'n':
++n;
break;
case 'i':
++i;
break;
case 'e':
++e;
break;
case 't':
++t;
break;
default:
break;
}
}

int minn = min({ i, e / 3, t, (n - 1) / 2 });

cout << minn << endl;
}

解析&吐槽:

一道水题。计算一段字符串中可以含有几个 nineteen 这个单词。需要注意的是这个单词的头和尾都是 n, 是可以连起来的。也就是说 nineteenineteen 算作两个单词。算法比较简单,统计所有的 n i e t 的个数, 除以它们在 nineteen 中出现的频率即可。对于 n ,我们可以列一个方程,两个单词需要 5 个 n, n 个单词需要 n*3-(n-1) = 2n+1 个 n,反之 x 个 n 最多可以拼成 (x-1)/2 个单词。找出所有的 字母可以拼出的单词数的最小值即可。

本作品采用 署名-相同方式共享 4.0 国际 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名 “不科学的科学君” (Liu233w) 与博客链接: https://liu233w.github.io ,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系

加载评论框需要翻墙