Oulipo
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 48387   Accepted: 19261

Description

The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote from the book:

Tout avait Pair normal, mais tout s’affirmait faux. Tout avait Fair normal, d’abord, puis surgissait l’inhumain, l’affolant. Il aurait voulu savoir où s’articulait l’association qui l’unissait au roman : stir son tapis, assaillant à tout instant son imagination, l’intuition d’un tabou, la vision d’un mal obscur, d’un quoi vacant, d’un non-dit : la vision, l’avision d’un oubli commandant tout, où s’abolissait la raison : tout avait l’air normal mais…

Perec would probably have scored high (or rather, low) in the following contest. People are asked to write a perhaps even meaningful text on some subject with as few occurrences of a given “word” as possible. Our task is to provide the jury with a program that counts these occurrences, in order to obtain a ranking of the competitors. These competitors often write very long texts with nonsense meaning; a sequence of 500,000 consecutive 'T's is not unusual. And they never use spaces.

So we want to quickly find out how often a word, i.e., a given string, occurs in a text. More formally: given the alphabet {'A''B''C', …, 'Z'} and two finite strings over that alphabet, a word W and a text T, count the number of occurrences of W in T. All the consecutive characters of W must exactly match consecutive characters of T. Occurrences may overlap.

Input

The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:

  • One line with the word W, a string over {'A''B''C', …, 'Z'}, with 1 ≤ |W| ≤ 10,000 (here |W| denotes the length of the string W).
  • One line with the text T, a string over {'A''B''C', …, 'Z'}, with |W| ≤ |T| ≤ 1,000,000.

Output

For every test case in the input file, the output should contain a single number, on a single line: the number of occurrences of the word W in the text T.

Sample Input

3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN

Sample Output

1
3
0
#include <iostream>
#include <stdio.h>
#include <cstring>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define huan printf("\n");
#define debug(a,b) cout<<a<<" "<<b<<" ";
using namespace std;
typedef long long ll;
const int maxn=1e6+,inf=0x3f3f3f3f;
const ll mod=1e9+;
char t[maxn],s[maxn];
int _next[maxn];
int tlen,slen;
void getnext()
{
int j,k;
j=,k=-,_next[]=-;
while(j<tlen)
if(k==-||t[j]==t[k])
_next[++j]=++k;
else
k=_next[k];
}
int KMP_count()
{
int ans=;
int i,j=;
if(slen==&&tlen==)
{
if(s[]==t[])
return ;
else
return ;
}
getnext();
for(i=;i<slen;i++)
{
while(j>&&s[i]!=t[j])
j=_next[j];
if(s[i]==t[j]) j++;
if(j==tlen)
{
ans++;j=_next[j];
}
}
return ans;
}
int main()
{
int _;
scanf("%d",&_);
while(_--)
{
scanf("%s",t);
scanf("%s",s);
tlen=strlen(t);
slen=strlen(s);
cout<<KMP_count()<<endl;
}
}

两个板子题

Number Sequence

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 38246    Accepted Submission(s): 15812

Problem Description
Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 10000, 1 <= N <= 1000000). Your task is to find a number K which make a[K] = b[1], a[K + 1] = b[2], ...... , a[K + M - 1] = b[M]. If there are more than one K exist, output the smallest one.
 
Input
The first line of input is a number T which indicate the number of cases. Each case contains three lines. The first line is two numbers N and M (1 <= M <= 10000, 1 <= N <= 1000000). The second line contains N integers which indicate a[1], a[2], ...... , a[N]. The third line contains M integers which indicate b[1], b[2], ...... , b[M]. All integers are in the range of [-1000000, 1000000].
 
Output
For each test case, you should output one line which only contain K described above. If no such K exists, output -1 instead.
 
Sample Input
2
13 5
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 1 3
13 5
1 2 1 2 3 1 2 3 1 3 2 1 2
1 2 3 2 1
 
Sample Output
6
-1
 #include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define huan printf("\n");
#define debug(a,b) cout<<a<<" "<<b<<" ";
using namespace std;
typedef long long ll;
const int maxn=1e6+,inf=0x3f3f3f3f;
const ll mod=1e9+;
int t[maxn],s[maxn];
int _next[maxn];
int tlen,slen;
void getnext()
{
int j,k;
j=,k=-,_next[]=-;
while(j<tlen)
if(k==-||t[j]==t[k])
_next[++j]=++k;
else
k=_next[k];
}
int KMP_index()
{
int i=,j=;
getnext();
while(i<slen&&j<tlen)
{
if(j==-||s[i]==t[j])
i++,j++;
else
j=_next[j];
}
if(j==tlen)
return i-tlen+;
else
return -;
}
int main()
{
int _;
scanf("%d",&_);
while(_--)
{
scanf("%d%d",&slen,&tlen);
for(int i=;i<slen;i++)
scanf("%d",&s[i]);
for(int i=;i<tlen;i++)
scanf("%d",&t[i]);
cout<<KMP_index()<<endl;
}
}

POJ 3461 字符串出现次数 && HDU1711 字符串第一次出现的位置 模板题的更多相关文章

  1. poj 2031 Building a Space Station【最小生成树prime】【模板题】

    Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5699   Accepte ...

  2. POJ C程序设计进阶 编程题#2:字符串中次数第2多的字母

    编程题#2:字符串中次数第2多的字母 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536k ...

  3. poj 3461 字符串单串匹配--KMP或者字符串HASH

    http://poj.org/problem?id=3461 先来一发KMP算法: #include <cstdio> #include <cstring> #include ...

  4. String 类中的几个练习--获取指定字符串中,大写字母、小写字母、数字的个数||获取一个字符串中,另一个字符串出现的次数

    package cn.homework.demo1; public class GetCount { /* * 获取一个字符串中,另一个字符串出现的次数 * 思想: * 1. indexOf到字符串中 ...

  5. Java 获取一个字符串中,另一个字符串出现的次数

    Java 获取一个字符串中,另一个字符串出现的次数 思想: 1. indexOf到字符串中到第一次出现的索引2. 找到的索引+被找字符串长度,截取字符串3. 计数器++ 代码实现: public cl ...

  6. PHP:strpos()-返回字符串在另一个字符串中第一次出现的位置

    strpos()函数返回字符串在另一个字符串中第一次出现的位置.如果没有找到该字符串,则返回false. 语法:strpos(sting, find [, start]) string ,必须,要搜索 ...

  7. 用Regex类计算一个字符串出现次数是最好方法【转载】

    我的一个朋友问我,怎么在c#或vb.net中,计算一个字符串中查找另一个字符串中出现的次数,他说在网上打了好多方法,我看了一下,有的是用replace的方法去实现,这种方法不是太好,占资源太大了.其实 ...

  8. Java 一个字符串在另外一个字符串出现次数

    统计一个字符串在另外一个字符串出现次数 代码如下: package me.chunsheng.javatest; import java.util.regex.Matcher; import java ...

  9. php 查找字符串里面中文字符第一次出现的位置,并插入字符串

    //查找字符串里面中文字符第一次出现的位置,并插入字符串 function find_first_chinese_insert($str,$insert_str){ $count = mb_strle ...

随机推荐

  1. Git使用简析

    推送本地操作 初始化一个本地Git仓库,在需要添加版本控制的文件夹根目录中使用git init命令. 添加文件到本地Git仓库: git add 文件名 # 添加文件到暂存区 git add . # ...

  2. 修改JRE system library

    MyEclipse 默认的情况下JRE system library 是:MyEclipse 的,如何修改工程中的JRE system library呢?步骤如下: 1.选择工程->Proper ...

  3. std::map插入已存在的key时,key对应的内容不会被更新

    std::map插入已存在的key时,key对应的内容不会被更新,如果不知道这一点,可能会造成运行结果与预期的不一致 “Because element keys in a map are unique ...

  4. 秒杀Sublime Text的微软开源代码编辑工具Visual Studio Code

    1. 下载链接: https://code.visualstudio.com/ 2. 秒开一个ASP.NET网站源码 3.编辑CSS颜色支持 4.Git支持 5.常用快捷键 Ctrl+Shift+P ...

  5. postgres的强制类型转换与时间函数

    一.类型转换postgres的类型转换:通常::用来做类型转换,timestamp到date用的比较多select  now()::dateselect  now()::varchar 示例1:日期的 ...

  6. [Python筆記] 將 Pandas 的 Dataframe 寫入 Sqlite3

    使用 pandas.io 寫入 Sqlite import sqlite3 as lite from pandas.io import sql import pandas as pd 依照 if_ex ...

  7. focus,focusin,blur,focusout区别

    focus与focusin 1.共同点:当 <div> 元素或其任意子元素获得焦点时执行事件 2.区别:focus不支持冒泡,而focusin支持冒泡: blur与focusout 1.共 ...

  8. <Spring Cloud>入门三 Ribbon

    1.Ribbon 客户端软负载均衡组件 1.1配置 搭建了三个消费者供客户端调用: 1.修改yml eureka: client: service-url: defaultZone: http://e ...

  9. MySQL丨02丨忘记root用户密码怎么办?

    软件:Mysql 版本:8.0.13 1. 先暂停mysql的服务,方法是在cmd里输入如下代码: net stop mysql 2. 在安装文件夹下创建一个文件:mysql-ini.txt (我的安 ...

  10. 【终极指南】图文详解Chrome插件离线安装方法

    Chrome插件离线安装背景介绍 因为无法访问Google所以国内用户目前大多只能通过第三方比如我们Chrome插件网下载插件,然后离线安装.Chrome官方自67版本后,只允许用户通过谷歌应用商店安 ...