BNU 13174 Substring Frequency
3C. Substring Frequency
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的更多相关文章
- lightoj 1427 - Substring Frequency (II) AC自动机
模板题,找来测代码. 注意有相同单词 //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<c ...
- Substring Frequency (II) LightOJ - 1427 AC自动机
https://vjudge.net/problem/LightOJ-1427 把所有模式串加入ac自动机,然后search的时候暴力,每个子串都暴力一下就好. 其实AC自动机就是,先建立好trie图 ...
- LeetCode Longest Substring with At Most Two Distinct Characters
原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/ 题目: Gi ...
- [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 ...
- [leetcode]3. Longest Substring Without Repeating Characters无重复字母的最长子串
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 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 ...
- leetcode difficulty and frequency distribution chart
Here is a difficulty and frequency distribution chart for each problem (which I got from the Interne ...
- Substring with Concatenation of All Words, 返回字符串中包含字符串数组所有字符串元素连接而成的字串的位置
问题描述:给定一个字符数组words,和字符串s,返回字符数组中所有字符元素组成的子串在字符串中的位置,要求所有的字符串数组里的元素只在字符串s中存在一次. 算法分析:这道题和strStr很类似.只不 ...
- *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 ...
随机推荐
- Lucas+中国剩余定理 HDOJ 5446 Unknown Treasure
题目传送门 题意:很裸,就是求C (n, m) % (p1 * p2 * p3 * .... * pk) 分析:首先n,m<= 1e18, 要用到Lucas定理求大组合数取模,当然p[]的乘积& ...
- 阻止默认行为是配合passive使用
在使用lighthouse检测pwa应用时,发现提示下面有下面的警告 默认使用passive:true提高滚动性能并减少崩溃,passive即顺从的,是指它顺从浏览器的默认行为.设置该属性的目的主要是 ...
- pyton 基础,运算符及字符类型。
一.python运算符: 二.数据类型: 1.数字: int :整型 32位机器上一个整数取值范围为-2**31~2**31-1即-2147483648~2147483647 64位机器上一个整数取 ...
- 关于margin、padding 对内联元素的影响
内联元素和块级元素的区别是新手必须要掌握的知识点.大家可能平时注意块级元素比较多.所以这里重点让我们来讲讲常见的width height margin padding 对inline元素的影响. 测 ...
- C#中this指针的用法示例
这篇文章主要介绍了C#中this指针的用法,对初学者而言是非常重要的概念,必须加以熟练掌握,需要的朋友可以参考下. 本文实例展示了C#中this指针的用法,对于初学者进一步牢固掌握C#有很大帮助,具体 ...
- 基于udp协议的套接字及udp协议粘包问题
udp协议的套接字 udp协议传输 服务端和客户端没有建立连接一说. import socket # 总结一下基础工作流程:服务端生成套接字并绑定ip_port,进入数据传输循环,服务端接受客户端发 ...
- js操作css样式、js的兼容问题
一.js操作css样式 div . style . width="200px" 在div标签内我们添加了一个style属性,并设定width值.这种写法会给标签带来大量的style ...
- JS进阶-特殊形式的函数-返回函数的函数/重写自己的函数
返回函数的函数 // 返回函数的函数 function a() { alert("aa"); return function () { alert("bb"); ...
- Ubuntu docker 使用命令 系列二
1.下载官方远程仓下的镜像:sudo docker pull <docker 镜像> ,sudo docker pull centos (没有指定版本,就是下载的最新的os) 2. 下载某 ...
- 【HEVC帧间预测论文】P1.7 Content Based Hierarchical Fast Coding Unit Decision Algorithm
Content Based Hierarchical Fast Coding Unit Decision Algorithm For HEVC <HEVC标准介绍.HEVC帧间预测论文笔记> ...