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. Eureka vs Zookeeper

    著名的CAP理论指出,一个分布式系统不可能同时满足C(一致性).A(可用性)和P(分区容错性).由于分区容错性在是分布式系统中必须要保证的,因此我们只能在A和C之间进行权衡.在此Zookeeper保证 ...

  2. Python中怎样用pip安装外部主机文件

    在python中安装非自带python模块.有三种方式: easy_install pip 下载压缩包(.zip, .tar, .tar.gz)后解压, 进入解压缩的文件夹后运行python setu ...

  3. hdu4921 Map

    给最多10条链.每条链长度最大1000,链上每点有权值,每条链上按顺序,第i个点属于level[i]. 链上后一个点能够选的前提是前面的点都选了. 选择了一些点能够得到的分数是两部分加起来:1.所有点 ...

  4. 经常使用 Java API

    经常使用Java API 一. java.io.BufferedReader类(用于从文件里读入一段字符.所属套件:java.io) 1. 构造函数BufferedReader(java.io.Fil ...

  5. 锁粒度 Deadlocks

    锁粒度 MySQL :: MySQL 5.7 Reference Manual :: 14.5.2.4 Locking Reads https://dev.mysql.com/doc/refman/5 ...

  6. VC FTP服务器程序分析(一)

    想在QT上移植一个FTP服务器程序,先学习windows下的FTP服务器例子,然后随便动手写点东西. 在pudn上搜索 "FTP服务器端和客户端实现 VC“这几个关键字,就可以搜到下面要分析 ...

  7. swt进度条 线程

    import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import java.util.Rando ...

  8. HDU 5438 Ponds

    Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Sub ...

  9. Redis持久化(RDB和AOF)

    什么是Redis持久化 什么是Redis持久化,就是将内存数据保存到硬盘. Redis 持久化存储 (AOF 与 RDB 两种模式) RDB持久化 RDB 是以二进制文件,是在某个时间 点将数据写入一 ...

  10. nodejs api 中文文档

    文档首页 英文版文档 本作品采用知识共享署名-非商业性使用 3.0 未本地化版本许可协议进行许可. Node.js v0.10.18 手册 & 文档 索引 | 在单一页面中浏览 | JSON格 ...