The 17th Zhejiang University Programming Contest Sponsored by TuSimple J
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的更多相关文章
- 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 ...
- 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 题解: 题意 ...
- 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 ...
- The 19th Zhejiang University Programming Contest Sponsored by TuSimple (Mirror)
http://acm.zju.edu.cn/onlinejudge/showContestProblems.do?contestId=391 A Thanks, TuSimple! Time ...
- 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 ...
- 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表示 ...
- The 18th Zhejiang University Programming Contest Sponsored by TuSimple -C Mergeable Stack
题目链接 题意: 题意简单,就是一个简单的数据结构,对栈的模拟操作,可用链表实现,也可以用C++的模板类来实现,但是要注意不能用cin cout,卡时间!!! 代码: #include <std ...
- 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 ...
- 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 ...
随机推荐
- ViewGroup如何分发事件
dispatchTouchEvent事件派发显示隧道方式.再是冒泡方式隧道方式传递,直道某一个元素消耗此事件,由上至下逐层分发视图.冒泡方式传递,当某个视图消耗事件后其return boolean 是 ...
- Apache Qpid Broker云
一. 什么是Broker云 Apathe Qpid 支持Broker Federation ,也就是Broker联盟或者叫做Broker云.Broker Federation可以通过配置消息路 ...
- O4编译 在 PingCAP 的一些技术挑战
在 PingCAP 的一些技术挑战 http://www.zenlife.tk/challenge-at-pingcap.md 在 PingCAP 的一些技术挑战 2018-06-02 事务优化 AC ...
- Exchangeable image file format
Exif - Wikipedia https://en.wikipedia.org/wiki/Exif
- 关于div li 等标签之间自带间距
可以用float来清除标签之间的间距. ps :ul使用font-size:0 唯一的缺点就是要再次设置LI的font-size
- Spark基本原理
仅作<Spark快速大数据分析>学习笔记 定义:Spark是一个用来实现 快速 而 通用 的集群计算平台:(通用的大数据处理引擎:) 改进了原Hadoop MapReduce处理模型,体现 ...
- Android系统设置Android adb 开关的方法【转】
本文转载自:http://www.wxtlife.com/2015/11/24/Android-set-adb-status/ 想第一时间获取我的最新文章,请关注公众号: 技术特工队 在整机系统开发中 ...
- bzoj5483: [Usaco2018 Dec]Balance Beam
又又又又又又又被踩爆了 首先容易写出这样的期望方程:f(1)=max(d(1),f(2)/2),f(n)=max(d(n),f(n-1)/2), f(i)=max(d(i),(f(i-1)+f(i+1 ...
- html5--6-7 CSS选择器4
html5--6-7 CSS选择器4 实例 学习要点 掌握常用的CSS选择器 了解不太常用的CSS选择器 什么是选择器 当我们定义一条样式时候,这条样式会作用于网页当中的某些元素,所谓选择器就是样式作 ...
- Apriori算法实例
Apriori算法与实例 R. Agrawal 和 R. Srikant于1994年在文献[2]中提出了Apriori算法,该算法的描述如下: 下面是一个具体的例子,最开始数据库里有4条交易,{A.C ...