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. dev右下角增加弹框提示信息

    using System; using System.Drawing; using System.IO; using System.Threading; using System.Windows.Fo ...

  2. git 代码冲突处理

    在使用git pull代码时,经常会碰到有冲突的情况,提示如下信息: error: Your local changes to 'c/environ.c' would be overwritten b ...

  3. [粒子特效]osg的自带粒子系统osgParticle::ParticleSystem

    osgParticle示例简单的演示了在osg中使用粒子系统的效果,用到了osgParticle库中的相关类,在osgParticle中主要有: (以下部分材料摘取自osg向场景中添加osgParti ...

  4. [调试][程序打印]当printf不能用时,使用C++的不定参数来搞定OutputDebugString打印

    void _trace( char* str , ...) //类似printf{ va_list vlist; va_start(vlist , str ); char a [100] ; vspr ...

  5. 《剑指offer》第二十二题(链表中倒数第k个结点)

    // 面试题22:链表中倒数第k个结点 // 题目:输入一个链表,输出该链表中倒数第k个结点.为了符合大多数人的习惯, // 本题从1开始计数,即链表的尾结点是倒数第1个结点.例如一个链表有6个结点, ...

  6. svg操纵方案 基于 D3 还是 angular?

    之前还是想简单了, 现在重新写这篇.把逻辑拆分粒度的辨析,放到外面去. 问题提出:svg控制方案 基于 D3 还是 angular 根据这个,html 4种展现样式:普通的html,svg,2D ca ...

  7. 30分钟了解Shiro与Springboot的多Realm基础配置

    写在前面的话: 我之前写过两篇与shiro安全框架有关的博文,居然能够广受欢迎实在令人意外.说明大家在互联网时代大伙对于安全和登录都非常重视,无论是大型项目还是中小型业务,普遍都至少需要登录与认证的逻 ...

  8. Codeforces 911E - Stack Sorting

    911E - Stack Sorting 思路: 用栈来模拟,能pop就pop,记下一个需要pop的数为temp,那么如果栈非空,栈顶肯定大于temp,那么加入栈 栈顶值-1 到 temp 的值,否则 ...

  9. [可能没有默认的字体]Warning: imagettfbbox() [function.imagettfbbox]: Invalid font filename...

    Warning: imagettfbbox() [function.imagettfbbox]: Invalid font filename... [可能没有默认的字体] 例: //putenv('G ...

  10. 20170706xlVBA根据工资汇总表生成个人工资条

    Sub NextSeven20170706001() Application.ScreenUpdating = False Application.DisplayAlerts = False Appl ...