Knuth-Morris-Pratt Algorithm


Time Limit: 1 Second      Memory Limit: 65536 KB

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.

题意:很明显的

解法:KMP

#include <iostream>
#include <cstring>
#include <cstdio> using namespace std;
char t[],s[];
int flink[];
void cmd(char *t)
{
int i=,j=-;
flink[]=-;
int len=strlen(t);
while(i<len)
{
if(j==- || t[i]==t[j])
flink[++i]=++j;
else
j=flink[j];
}
}
int sum(char *t,char *s)
{
int ans=;
int i=,j=;
int n=strlen(t);
int len;
len=strlen(s);
while(i<len)
{
if(j==- || s[i]==t[j])
{
++i;
++j;
}
else
{
j=flink[j];
}
if(j==n) ans++;
}
return ans;
}
int main()
{
int c;
scanf("%d",&c);
while(c--)
{
scanf("%s",s);
cmd("cat");
int a=sum("cat",s);
cmd("dog");
int b=sum("dog",s);
printf("%d\n",a+b);
}
return ;
}

The 17th Zhejiang University Programming Contest Sponsored by TuSimple J的更多相关文章

  1. The 17th Zhejiang University Programming Contest Sponsored by TuSimple A

    Marjar Cola Time Limit: 1 Second      Memory Limit: 65536 KB Marjar Cola is on sale now! In order to ...

  2. zoj 4020 The 18th Zhejiang University Programming Contest Sponsored by TuSimple - G Traffic Light(广搜)

    题目链接:The 18th Zhejiang University Programming Contest Sponsored by TuSimple - G Traffic Light 题解: 题意 ...

  3. The 19th Zhejiang University Programming Contest Sponsored by TuSimple (Mirror) B"Even Number Theory"(找规律???)

    传送门 题意: 给出了三个新定义: E-prime : ∀ num ∈ E,不存在两个偶数a,b,使得 num=a*b;(简言之,num的一对因子不能全为偶数) E-prime factorizati ...

  4. The 19th Zhejiang University Programming Contest Sponsored by TuSimple (Mirror)

    http://acm.zju.edu.cn/onlinejudge/showContestProblems.do?contestId=391 A     Thanks, TuSimple! Time ...

  5. Mergeable Stack 直接list内置函数。(152 - The 18th Zhejiang University Programming Contest Sponsored by TuSimple)

    题意:模拟栈,正常pop,push,多一个merge A B 形象地说就是就是将栈B堆到栈A上. 题解:直接用list 的pop_back,push_back,splice 模拟, 坑:用splice ...

  6. 152 - - G Traffic Light 搜索(The 18th Zhejiang University Programming Contest Sponsored by TuSimple )

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5738 题意 给你一个map 每个格子里有一个红绿灯,用0,1表示 ...

  7. The 18th Zhejiang University Programming Contest Sponsored by TuSimple -C Mergeable Stack

    题目链接 题意: 题意简单,就是一个简单的数据结构,对栈的模拟操作,可用链表实现,也可以用C++的模板类来实现,但是要注意不能用cin cout,卡时间!!! 代码: #include <std ...

  8. The 18th Zhejiang University Programming Contest Sponsored by TuSimple

    Pretty Matrix Time Limit: 1 Second      Memory Limit: 65536 KB DreamGrid's birthday is coming. As hi ...

  9. ZOJ 4016 Mergeable Stack(from The 18th Zhejiang University Programming Contest Sponsored by TuSimple)

    模拟题,用链表来进行模拟 # include <stdio.h> # include <stdlib.h> typedef struct node { int num; str ...

随机推荐

  1. ubuntu git ssh不通

    xyh@ubuntu-64:~$ ssh -v git@danxinben.com                                                            ...

  2. (转载)常用的Mysql数据库操作语句大全

    打开CMD,进入数据库命令:mysql -hlocalhost -uroot -p 退出数据库:exit 用户管理: 1.新建用户: >CREATE USER name IDENTIFIED B ...

  3. WPF绑定各种数据源之object数据源

    一.WPF绑定各种数据源索引 WPF 绑定各种数据源之Datatable WPF绑定各种数据源之object数据源 WPF绑定各种数据源之xml数据源 WPF绑定各种数据源之元素控件属性 Bindin ...

  4. rails用generate为两个模型创建has_and_belongs_to_many中间表

    假设 teachers和students具备many-to-many的关系,那么需要一个Join 表,has_and_belongs_to_many默认该表名字为teachers_students,这 ...

  5. 【转】使用git 工具下载android.jar Source Code

    为了开发android应用,在开发时发现sdk没有源代码,这样在开发时太麻烦了,下面说说如何下载源代码,以及如何配置. 下载源代码需要git,先下载一个git.下面的操作都是在windows下完成的. ...

  6. 重入锁ReentrantLock用法以及如何实现重进入

    在java多线程中,可以使用synchronized实现线程之间的同步互斥,但在jdk1.5中增加了ReentrantLock类也能达到同样的效果,而且在使用上更加灵活,扩展功能上更加强大. 创建My ...

  7. WAS:节点不同步问题

    刀片服务器硬盘坏了,换了硬盘后,通过dmgr无法重启该节点上的server. 单机./starServer 后,服务虽然启动了,但后台一直提示如下: [-- ::: CST] RoleViewLead ...

  8. [原创]java获取word文档的条目化内容

    在开发Web办公系统或文档系统时,PageOffice组件是众所周知的在线处理微软word/ppt/excel文档的强大工具,它对WORD文档的各种处理在API层面进行了封装,屏蔽了Office VB ...

  9. C#参数数组的用法1

    C# 参数数组 有时,当声明一个方法时,您不能确定要传递给函数作为参数的参数数目.C# 参数数组解决了这个问题,参数数组通常用于传递未知数量的参数给函数. params 关键字 在使用数组作为形参时, ...

  10. uCareSystem:Ubuntu/Linux Mint的一体化系统更新和维护工具

    对于任何一款允许用户还原电脑到之前状态(包括文件系统,安装的应用,以及系统设置)的操作系统来说,系统还原功能都是必备功能,它可以恢复系统故障以及其他的问题. 有的时候安装一个程序或者驱动可能让你的系统 ...