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的使用。。。。。。。。。。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <climits>
  7. #include <vector>
  8. #include <queue>
  9. #include <cstdlib>
  10. #include <string>
  11. #include <set>
  12. #define LL long long
  13. #define INF 0x3f3f3f3f
  14. using namespace std;
  15. int fail[];
  16. char str[],p[];
  17. void getFail(int &len){
  18. fail[] = fail[] = ;
  19. len = strlen(p);
  20. for(int i = ; i < len; i++){
  21. int j = fail[i];
  22. while(j && p[i] != p[j]) j = fail[j];
  23. fail[i+] = p[i] == p[j]?j+:;
  24. }
  25. }
  26. int main(){
  27. int t,i,j,len,ans,k = ;
  28. scanf("%d",&t);
  29. while(t--){
  30. scanf("%s%s",str,p);
  31. getFail(len);
  32. j = ans = ;
  33. for(i = ; str[i]; i++){
  34. while(j && str[i] != p[j]) j = fail[j];
  35. if(str[i] == p[j]) j++;
  36. if(j == len) ans++;
  37. }
  38. printf("Case %d: %d\n",k++,ans);
  39. }
  40. return ;
  41. }

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. tsconfig.json No inputs were found in config file

    Build:No inputs were found in config file '/tsconfig.json'. Specified 'include' paths were '["* ...

  2. Retrofit实现PUT网络请求,并修改Content-Type

    @FormUrlEncoded @PUT(Constant.BOSS_HX_CHANGE_PHONE_INTERVIEW) Call<ResponseHxResultBean> handl ...

  3. HtmlUnit爬取Ajax动态生成的页面内容

    HtmlUnit说白了就是一个浏览器,这个浏览器是用Java写的无界面的浏览器,正因为其没有界面,因此执行的速度还是可以滴. HtmlUnit提供了一系列的API,这些API可以干的功能比较多,如表单 ...

  4. [转]Android TCP长连接 心跳机制及实现

    背景知识 智能手机上的长连接心跳和在Internet上的长连接心跳有什么不同 Android系统的推送和iOS的推送有什么区别 几种推送的实现方式 协议 1XMPP简介 2 MQTT简介 3移动端消息 ...

  5. pandas中loc-iloc-ix的使用

    转自:https://www.jianshu.com/p/d6a9845a0a34 Pandas中loc,iloc,ix的使用 使用 iloc 从DataFrame中筛选数据 iloc 是基于“位置” ...

  6. Java 11 正式发布,支持期限至2026年9月

    美国当地时间9月25日,Oracle 官方宣布 Java 11 (18.9 LTS) 正式发布,可在生产环境中使用!这是自 Java 8 后的首个长期支持版本,非常值得大家的关注,可以通过下面的地址进 ...

  7. RHEL5.8上SAMBA源码修改打包安装流程

    之前一直使用系统自带的SAMBA,近期需要对SAMBA代码做一些修改,然后还是打算用RPM包的方式来安装部署. 这个流程本身不复杂,在这里记录下来,免得在另外写说明文档. 关键词:RHEL5.8, s ...

  8. IE11/Flash页游白屏怎么办!立刻开启IE大地址模式!缓解浏览器白屏问题

    您是否经常发现IE白屏了,具体表现为点开新网页时无法显示,只能切换标签,用任务管理器一看,内存1.2G之多. 这是因为IE11可能有内存泄露问题,内存不断增长以至于无法申请新的内存,于是IE就完蛋了! ...

  9. 关于dzzoffice 破解版

    最近看到很多人在搜索dzzoffice破解版,其实dzzoffie是一款全开源的产品,开放的功能是与演示站中一摸一样的,所以并不会有人破解这种全开源的系统.那么为什么会有人搜索这样的关键词呢? 可能大 ...

  10. (转)使用Spring的注解方式实现AOP的细节

    http://blog.csdn.net/yerenyuan_pku/article/details/52879669 前面我们已经入门使用Spring的注解方式实现AOP了,现在我们再来学习使用Sp ...