POJ 3461 字符串出现次数 && HDU1711 字符串第一次出现的位置 模板题
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
#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 字符串第一次出现的位置 模板题的更多相关文章
- poj 2031 Building a Space Station【最小生成树prime】【模板题】
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5699 Accepte ...
- POJ C程序设计进阶 编程题#2:字符串中次数第2多的字母
编程题#2:字符串中次数第2多的字母 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536k ...
- poj 3461 字符串单串匹配--KMP或者字符串HASH
http://poj.org/problem?id=3461 先来一发KMP算法: #include <cstdio> #include <cstring> #include ...
- String 类中的几个练习--获取指定字符串中,大写字母、小写字母、数字的个数||获取一个字符串中,另一个字符串出现的次数
package cn.homework.demo1; public class GetCount { /* * 获取一个字符串中,另一个字符串出现的次数 * 思想: * 1. indexOf到字符串中 ...
- Java 获取一个字符串中,另一个字符串出现的次数
Java 获取一个字符串中,另一个字符串出现的次数 思想: 1. indexOf到字符串中到第一次出现的索引2. 找到的索引+被找字符串长度,截取字符串3. 计数器++ 代码实现: public cl ...
- PHP:strpos()-返回字符串在另一个字符串中第一次出现的位置
strpos()函数返回字符串在另一个字符串中第一次出现的位置.如果没有找到该字符串,则返回false. 语法:strpos(sting, find [, start]) string ,必须,要搜索 ...
- 用Regex类计算一个字符串出现次数是最好方法【转载】
我的一个朋友问我,怎么在c#或vb.net中,计算一个字符串中查找另一个字符串中出现的次数,他说在网上打了好多方法,我看了一下,有的是用replace的方法去实现,这种方法不是太好,占资源太大了.其实 ...
- Java 一个字符串在另外一个字符串出现次数
统计一个字符串在另外一个字符串出现次数 代码如下: package me.chunsheng.javatest; import java.util.regex.Matcher; import java ...
- php 查找字符串里面中文字符第一次出现的位置,并插入字符串
//查找字符串里面中文字符第一次出现的位置,并插入字符串 function find_first_chinese_insert($str,$insert_str){ $count = mb_strle ...
随机推荐
- php 缓存工具类 实现网页缓存
php 缓存工具类 实现网页缓存 php程序在抵抗大流量访问的时候动态网站往往都是难以招架,所以要引入缓存机制,一般情况下有两种类型缓存 一.文件缓存 二.数据查询结果缓存,使用内存来实现高速缓存 本 ...
- mac自带终端安装完ohmyZsh后显示乱码
修改描述文件-添加 选择新导入的 Meslo LG M Regular for Powerline
- IOS存储目录documents你在哪里啊
iphone沙箱模型的有四个文件夹,分别是什么,永久数据存储一般放在什么位置,得到模拟器的路径的简单方式是什么. documents,tmp,app,Library. (NSHomeDirectory ...
- php用面向对象从mysql取数据
<?php //建立数据库的链接@$_mysqli = new mysqli('localhost','root','123456','dbname');if(mysqli_connect_er ...
- 洛谷 P2319 [HNOI2006]超级英雄
题目描述 题目描述 现在电视台有一种节目叫做超级英雄,大概的流程就是每位选手到台上回答主持人的几个问题,然后根据回答问题的多少获得不同数目的奖品或奖金.主持人问题准备了若干道题目,只有当选手正确回答一 ...
- docker 新手入门 (web项目的部署)
web项目的部署 1.首先我们下载centos镜像.docker pull centos 2.下载完成之后,我们首先要安装的是java环境 tomcat 和jdk 3.将下载好的软件放入到nmt目录 ...
- PHP中session和cookie的区别
这个话题无论是系统运维还是PHP开发人员面试时会经常遇到,所以这里也进行一些总结和归纳,session和cookie的具体理论网上比较大,大家googel下均可;系统运维注意区分下session(会话 ...
- 旅行商问题——状态压缩DP
问题简介 有n个城市,每个城市间均有道路,一个推销员要从某个城市出发,到其余的n-1个城市一次且仅且一次,然后回到再回到出发点.问销售员应如何经过这些城市是他所走的路线最短? 用图论的语言描述就是:给 ...
- vscode F12 不能用,原来是快捷键冲突了。
vscode F12 不能用,原来是快捷键冲突了.
- Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/config/spring/applicationContext.xml]
在搭建SpringMVC框架的时候遇到了这个问题 问题的原因: 就是没有找到applicatoincontext.xml这个文件, 因为idea自动生成的路径不正确 因此需要再web.xml里面, ( ...