3C. Substring Frequency

Time Limit: 1000ms
Memory Limit: 32768KB

64-bit integer IO format: %lld      Java class name: Main

 
 

A string is a finite sequence of symbols that are chosen from an alphabet. In this problem you are given two non-empty strings Aand B, both contain lower case English characters. You have to find the number of times B occurs as a substring of A.

 

Input

Input starts with an integer T (≤ 5), denoting the number of test cases.

Each case starts with two lines. First line contains A and second line contains B. You can assume than 1 ≤ length(A), length(B) ≤ 106.

 

Output

For each case, print the case number and the number of times B occurs as a substring of A.

 

Sample Input

4

axbyczd

abc

abcabcabcabc

abc

aabacbaabbaaz

aab

aaaaaa

aa

Sample Output

Case 1: 0

Case 2: 4

Case 3: 2

Case 4: 5

解题:裸KMP的使用。。。。。。。。。。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
int fail[];
char str[],p[];
void getFail(int &len){
fail[] = fail[] = ;
len = strlen(p);
for(int i = ; i < len; i++){
int j = fail[i];
while(j && p[i] != p[j]) j = fail[j];
fail[i+] = p[i] == p[j]?j+:;
}
}
int main(){
int t,i,j,len,ans,k = ;
scanf("%d",&t);
while(t--){
scanf("%s%s",str,p);
getFail(len);
j = ans = ;
for(i = ; str[i]; i++){
while(j && str[i] != p[j]) j = fail[j];
if(str[i] == p[j]) j++;
if(j == len) ans++;
}
printf("Case %d: %d\n",k++,ans);
}
return ;
}

BNU 13174 Substring Frequency的更多相关文章

  1. lightoj 1427 - Substring Frequency (II) AC自动机

    模板题,找来测代码. 注意有相同单词 //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<c ...

  2. Substring Frequency (II) LightOJ - 1427 AC自动机

    https://vjudge.net/problem/LightOJ-1427 把所有模式串加入ac自动机,然后search的时候暴力,每个子串都暴力一下就好. 其实AC自动机就是,先建立好trie图 ...

  3. LeetCode Longest Substring with At Most Two Distinct Characters

    原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/ 题目: Gi ...

  4. [leetcode]76. Minimum Window Substring最小字符串窗口

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  5. [leetcode]3. Longest Substring Without Repeating Characters无重复字母的最长子串

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  6. LeetCode解题报告—— Minimum Window Substring && Largest Rectangle in Histogram

    1. Minimum Window Substring Given a string S and a string T, find the minimum window in S which will ...

  7. leetcode difficulty and frequency distribution chart

    Here is a difficulty and frequency distribution chart for each problem (which I got from the Interne ...

  8. Substring with Concatenation of All Words, 返回字符串中包含字符串数组所有字符串元素连接而成的字串的位置

    问题描述:给定一个字符数组words,和字符串s,返回字符数组中所有字符元素组成的子串在字符串中的位置,要求所有的字符串数组里的元素只在字符串s中存在一次. 算法分析:这道题和strStr很类似.只不 ...

  9. *187. Repeated DNA Sequences (hashmap, one for loop)(difference between subsequence & substring)

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

随机推荐

  1. LCA+树状数组 POJ 2763 Housewife Wind

    题目传送门 题意:两种操作,问u到v的距离,并且u走到了v:把第i条边距离改成w 分析:根据DFS访问顺序,将树处理成链状的,那么回边处理成负权值,那么LCA加上BIT能够知道u到v的距离,BIT存储 ...

  2. (3)《Head First HTML与CSS》学习笔记---CSS入门

    1.O‘Reilly的<CSS PocketReference>是一本不错的CSS参考小书,记录了常用的元素属性. 2.元素选择器的作用强于继承的作用:用户定义强于浏览器默认(以下所有讨论 ...

  3. 26款优秀的Android逆向工程工具

    26款优秀的Android逆向工程工具

  4. 【数据分析 R语言实战】学习笔记 第七章 假设检验及R实现

    假设检验及R实现 7.1假设检验概述 对总体参数的具体数值所作的陈述,称为假设;再利用样本信息判断假设足否成立,这整个过程称为假设检验. 7.1.1理论依据 假设检验之所以可行,其理沦背景是小概率理论 ...

  5. java访问数据库步骤详解

    eg1: public static void main(String[] args) throws ClassNotFoundException, SQLException { //第一步:加载JD ...

  6. centos系统iptables使用帮助

    #如果只是想屏蔽IP的话“开放指定的端口”可以直接跳过.#屏蔽单个IP的命令是iptables -I INPUT -s 123.45.6.7 -j DROP#封整个段即从123.0.0.1到123.2 ...

  7. Redis学习笔记(四)集合进阶

    1.组合与关联多个集合 差集: SDIFF key1 [key2...](返回存在于key1但不存在其他集合中的元素) SDIFFSTORE destination key1 [key2...](将存 ...

  8. sql server 2000备份还原数据库

    转载请注明出处:http://blog.csdn.net/neochan1108/article/details/79248017 备份: -- Create the backup device fo ...

  9. 推荐一款功能强大的Tomcat 管理监控工具,可替代Tomcat Manager

    我们在本地启动Tomcat服务器后,用localhost:访问: 再点Manager App,即可进入Tomcat自带的Manager这个应用,此处可以单独部署/卸载每一个应用.可以看到在Manage ...

  10. DROP USER - 删除一个数据库用户帐号

    SYNOPSIS DROP USER name DESCRIPTION 描述 DROP USER 从数据库中删除指定的用户. 它不删除数据库里此用户所有的表,视图或其他对象. 如果该用户拥有任何数据库 ...