当前位置:   article > 正文

C. Rudolf and the Ugly String

C. Rudolf and the Ugly String

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Rudolf has a string s� of length n�. Rudolf considers the string s� to be ugly if it contains the substring†† "pie" or the substring "map", otherwise the string s� will be considered beautiful.

For example, "ppiee", "mmap", "dfpiefghmap" are ugly strings, while "mathp", "ppiiee" are beautiful strings.

Rudolf wants to shorten the string s� by removing some characters to make it beautiful.

The main character doesn't like to strain, so he asks you to make the string beautiful by removing the minimum number of characters. He can remove characters from any positions in the string (not just from the beginning or end of the string).

†† String a� is a substring of b� if there exists a consecutive segment of characters in string b� equal to a�.

Input

The first line contains a single integer t� (1≤t≤1041≤�≤104) — the number of test cases. The descriptions of the test cases follow.

The first line of each test case contains a single integer n� (1≤n≤1061≤�≤106) — the length of the string s�.

The next line of each test case contains the string s� of length n�. The string s� consists of lowercase Latin letters.

The sum of n� over all test cases does not exceed 106106.

Output

For each test case, output a single integer — the minimum number of characters that need to be deleted to make the string s� beautiful. If the string is initially beautiful, then output 00.

Example

input

Copy

 

6

9

mmapnapie

9

azabazapi

8

mappppie

18

mapmapmapmapmapmap

1

p

11

pppiepieeee

output

Copy

2
0
2
6
0
2

Note

In the first test case, for example, you can delete the 44th and 99th characters to make the string beautiful.

In the second test case, the string is already beautiful.

解题说明:此题是一道字符串题,直接遍历,每次读取三个字母,如果存在不满足的字符串,剔除中间的字母,继续遍历。

  1. #include<stdio.h>
  2. char s[1000001];
  3. int main()
  4. {
  5. int t, n;
  6. scanf("%d", &t);
  7. while (t--)
  8. {
  9. scanf("%d", &n);
  10. scanf("%s", s);
  11. int u1 = 0;
  12. for (int i = 0; i < n; i++)
  13. {
  14. if ((s[i] == 'm' && s[i + 1] == 'a' && s[i + 2] == 'p') || (s[i] == 'p' && s[i + 1] == 'i' && s[i + 2] == 'e'))
  15. {
  16. u1++;
  17. i += 2;
  18. }
  19. }
  20. printf("%d\n", u1);
  21. }
  22. return 0;
  23. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/372582
推荐阅读
相关标签
  

闽ICP备14008679号