In computer science, the Knuth-Morris-Pratt string searching algorithm (or KMP algorithm) searches for occurrences of a "word" W within a main "text string" S by employing the observation that when a mismatch occurs, the word itself embodies sufficient information to determine where the next match could begin, thus bypassing re-examination of previously matched characters.

Edward is a fan of mathematics. He just learnt the Knuth-Morris-Pratt algorithm and decides to give the following problem a try:

Find the total number of occurrence of the strings "cat" and "dog" in a given string s.

As Edward is not familiar with the KMP algorithm, he turns to you for help. Can you help Edward to solve this problem?

Input

There are multiple test cases. The first line of input contains an integer T (1 ≤ T ≤ 30), indicating the number of test cases. For each test case:

The first line contains a string s (1 ≤ |s| ≤ 1000).

Output

For each case, you should output one integer, indicating the total number of occurrence of "cat" and "dog" in the string.

Sample Input

7
catcatcatdogggy
docadosfascat
dogdddcat
catcatcatcatccat
dogdogdogddddooog
dcoagtcat
doogdog

Sample Output

4
1
2
5
3
1
1

Hint

For the first test case, there are 3 "cat" and 1 "dog" in the string, so the answer is 4.

For the second test case, there is only 1 "cat" and no "dog" in the string, so the answer is 1.

水题

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
using namespace std; char a[]; int main()
{
int T,len,sum,i;
while(~scanf("%d",&T))
{
while(T--)
{
scanf("%s",&a);
len=strlen(a);
sum=;
for(i=;i<len-;i++)
{
if(a[i]=='c'&&a[i+]=='a'&&a[i+]=='t')
sum++;
else if(a[i]=='d'&&a[i+]=='o'&&a[i+]=='g')
sum++;
}
printf("%d\n",sum);
}
}
return ;
}

ZOJ 17届校赛 Knuth-Morris-Pratt Algorithm( 水题)的更多相关文章

  1. ZOJ 17届校赛 How Many Nines

    If we represent a date in the format YYYY-MM-DD (for example, 2017-04-09), do you know how many 9s w ...

  2. HDU 6077 17多校4 Time To Get Up 水题

    Problem Description Little Q's clock is alarming! It's time to get up now! However, after reading th ...

  3. 我所理解的 KMP(Knuth–Morris–Pratt) 算法

    假设要在 haystack 中匹配 needle . 要理解 KMP 先需要理解两个概念 proper prefix 和 proper suffix,由于找到没有合适的翻译,暂时分别称真实前缀 和 真 ...

  4. 字符串匹配算法--KMP字符串搜索(Knuth–Morris–Pratt string-searching)C语言实现与讲解

    一.前言   在计算机科学中,Knuth-Morris-Pratt字符串查找算法(简称为KMP算法)可在一个主文本字符串S内查找一个词W的出现位置.此算法通过运用对这个词在不匹配时本身就包含足够的信息 ...

  5. CSUST 第15届 校赛总结

    一直想记录一下自己的比赛,却感觉空间说说有点不适,思考了一番还是打算放到自己的博客园 这次比赛总体来说还是不错,签到还是稳的一批,基本前四小时都在rk1 开局切了几道签到题,然后开了一道思维gcd,正 ...

  6. 福州大学第十届校赛 & fzu 2128最长子串

    思路: 对于每个子串,求出 母串中 所有该子串 的 开始和结束位置,保存在 mark数组中,求完所有子串后,对mark数组按 结束位置排序,然后 用后一个的结束位置 减去 前一个的 开始 位置 再 减 ...

  7. 广工十四届校赛 count 矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6470 题意:求,直接矩阵快速幂得f(n)即可 构造矩阵如下: n^3是肯定得变换的,用二项式展开来一点 ...

  8. 第五届华中区程序设计邀请赛暨武汉大学第十四届校赛 网络预选赛 A

    Problem 1603 - Minimum Sum Time Limit: 2000MS   Memory Limit: 65536KB   Total Submit: 564  Accepted: ...

  9. 之江学院第0届校赛 qwb去面试 (找规律)

    Description 某一天,qwb去WCfun面试,面试官问了他一个问题:把一个正整数n拆分成若干个正整数的和,请求出这些数乘积的最大值. qwb比较猥琐,借故上厕所偷偷上网求助,聪明的你能帮助他 ...

随机推荐

  1. spring +servlet 与 spring MVC

    spring serlvetSpring框架中context-param与servlet中init-param的contextConfigLocation的区别https://blog.csdn.ne ...

  2. Js页面自动跳转

    //声明 t = 1 var t = 10; function openwin() { t -= 1; if(t==0){ location.href='index2.html'; } setTime ...

  3. Python day13文件的读写

    # 文件操作 f=open("E:\\1.txt",encoding="GBK")#打开文件 print(f.writable())#是否可写 print(f. ...

  4. Oracle 千万级别数据查询优化

    说明:平时很少接触到大数据分页,今天有兴趣在数据库插入1000万条数据进行测试,经过查询相关资料得到如下说明:笔者在工作中有一上百万条记录的表,在jsp页面中需对该表进行分页显示,便考虑用rownum ...

  5. spring boot 2.0 + 静态资源被拦截,怎么办?

    问题描述:使用springboot 2.0后,按照springboot 1.5版本(以下简称旧版)的方式去配置项目.结果发现静态资源访问不到了,本文对此情况分析.处理 项目结构: 直接上图 如果是在旧 ...

  6. String字符串存入数据库中超出最大长度(oracle varchar2 4000)?应合理分条存储(java实现-工具/方法)

    问题描述 需要向数据库中保存数据,但某个字段内容长度过长(有中文.符号.英文),应该根据字符串内容与数据库存储上限合理设置储存方式. 解决思路 分条存储,即多条数据前n个字段一致,最后内容字段不同,下 ...

  7. Silverlight自定义控件系列 – TreeView (1)

      原文路径:http://blog.csdn.net/wlanye/article/details/7265457 很多人都对MS自带的控件不太满意(虽然MS走的是简约风格),都会试图去修改或创建让 ...

  8. PHP函数总结 (三)

    <?php/** * PHP变量的范围 * 1.局部变量(内部变量) * 在函数内部声明的变量,作用域仅限于函数内部,参数也是局部变量:执行完毕后函数内部的变量都被释放 * 若需要使用函数内的变 ...

  9. Codeforces Round #364 (Div. 1) (差一个后缀自动机)

    B. Connecting Universities 大意: 给定树, 给定2*k个点, 求将2*k个点两两匹配, 每个匹配的贡献为两点的距离, 求贡献最大值 单独考虑每条边$(u,v)$的贡献即可, ...

  10. splay板子

    1, splay的一些基本操作. 使用前要插入$-INF,+INF$保证每个点的前驱后继存在. $get$函数在$x$存在时, 调用后, 根为$x$, 否则根为$x$的前驱或后继 const int ...