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. 初探 Yii2 的测试模式 index-test.php

    有没有发现高级版每个应用的 web 目录下有两个入口文件,一个index.php 一个 index-test.php通过init.bat可以切换到调试模式和产品模式,这两个模式相信同学们都很熟悉了,那 ...

  2. js delete可以删除对象属性及变量

    ,对象属性删除 function fun(){ this.name = 'mm'; } var obj = new fun(); console.log(obj.name);//mm delete o ...

  3. Java东西太多,记录一些知识点

    实习两个月了,这两个月接触了不少东西,简单列举一下知识,未来需要多多学习和了解. 1.前端js.extjs4.Jquery(js框架这些基本现学现用): 2.基础不好要补补Servlet和JSP(再往 ...

  4. selenium-chrome-headless

    #coding=utf-8 from selenium import webdriver import time chrome_options = webdriver.ChromeOptions() ...

  5. Jmeter 同一个测试计划下的多个线程组 执行顺序 希望调整为顺序执行

    用Jmeter做自动化测试,一个测试计划中添加多个线程组, 每个线程组的功能测试,希望是一个线程组执行完毕后,接着执行下一个线程组下的请求 Jmeter默认多个线程组之间是并行关系 需要在测试计划下勾 ...

  6. centos 6.5 安装mplayer

    https://centos.pkgs.org/6/linuxtech/mplayer-1.0.4-3.el6.x86_64.rpm.html

  7. Redis 安装到linux系统

    下载地址 : http://download.redis.io/releases/redis-3.0.3.tar.gz ). tar -zxvf redis-.tar.gz -C /usr/share ...

  8. SQL Timeout超时的处理方法

    第一步:修改Web.config配置文件.在数据库连接字符串中加上连接时间Connect Timeout,根据实际情况定时间. <!--连接数据库--> <connectionStr ...

  9. 工程优化暨babel升级小记

    小记背景 随着业务代码的增多,项目代码的编译时长也在增多,遂针对这个痛点在dev下做些优化 第一部分:优化dev编译时间 这里优化的主要思路是在dev环境下,单独出来一个dll配置文件,将项目中的部分 ...

  10. inputsimulator - Windows Input Simulator

    窗体输入模拟器提供一个基于 win32 SendInput  方法的 模拟键盘鼠标输入的.net 接口.windows 输入模拟器可用于 WPF.windows 窗体和控制台应用程序, 实现模拟任意按 ...