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 ...
随机推荐
- 自定义 TypeHandler
自定义TypeHandler分为三个步骤: 1.编写自定义TypeHandler,并继承自抽象类BaseTypeHandler<T>,实现抽象方法 2.在mybatis-config.xm ...
- springboot的多个配置文件的关系
一般我们在使用springboot时会用到多个环境下的配置文件,例如 application-dev.yml:开发环境 application-uat.yml:用户验收测试环境 application ...
- 如何在运行时改变App的图标
在你完成应用程序的beta版本后,最后会有些人去帮你测试,使你去完善应用程序……或者会有投资青睐.但是如果测试人员有一种简单地方式去检查构建版本的应用程序会不会有帮助呢? 这个教程将会向你展示这些,向 ...
- docker 深入理解之cgroups
cgroups 资源限制 cgroups 是什么 cgroups 最初名为process container,有Google工程师Paul Menage和Rohit Seth于 2006 年提出,后由 ...
- Spring Data Redis入门示例:字符串操作(六)
Spring Data Redis对字符串的操作,封装在了ValueOperations和BoundValueOperations中,在集成好了SPD之后,在需要的地方引入: // 注入模板操作实例 ...
- SQL Sever中多列拼接成一列值为NULL
查询出数据 SELECT a.ID AS KYMain_ID , ',' + a.Leader + ',' AS KYMain_Leader , ), b.TaskLeader) FROM TB_KY ...
- N皇后递归
问题: n皇后问题:输入整数n, 要求n个国际象棋的皇后,摆在 n*n的棋盘上,互相不能攻击,输出全部方案. #include <iostream> using namespace std ...
- 使用Spring IoC进行Bean装配
Spring概述 Spring的设计严格遵从的OCP(开闭原则),保证对修改的关闭,也就是外部无法改变spring内部的运行流程:提供灵活的扩展接口,也就是可以通过extends,implements ...
- linux批量检测服务器能否ping通和硬盘容量状态并抛出报警的一个脚本-附详细解释
有一些linux基础,最近刚开始学shell,参考了阿良老师的一个监测服务器硬盘状态的脚本,自己进行了一些扩展,今天比较晚了,后边会把注释放上来,感觉脚本还很不完善,希望大家一起探讨一下,共同学习 2 ...
- maven构建springmvc项目
1.Eclipse中 NEW ->OTHER->Maven->maven project 2.选择项目路径 3.选择项目类型->next->输入groupid和artif ...