KMP——模板 练手题

1. 洛谷  P3375 【模板】KMP字符串匹配

题目描述

如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置。

为了减少骗分的情况,接下来还要输出子串的前缀数组next。如果你不知道这是什么意思也不要问,去百度搜[kmp算法]学习一下就知道了。

输入输出格式

输入格式:

第一行为一个字符串,即为s1(仅包含大写字母)

第二行为一个字符串,即为s2(仅包含大写字母)

输出格式:

若干行,每行包含一个整数,表示s2在s1中出现的位置

接下来1行,包括length(s2)个整数,表示前缀数组next[i]的值。

输入输出样例

输入样例#1:

ABABABC
ABA

输出样例#1:

1
3
0 0 1 

说明

时空限制:1000ms,128M

数据规模:

设s1长度为N,s2长度为M

对于30%的数据:N<=15,M<=5

对于70%的数据:N<=10000,M<=100

对于100%的数据:N<=1000000,M<=1000

样例说明:

所以两个匹配位置为1和3,输出1、3

血的教训啊,调了一上午的代码,居然错在了输入上,谨记大佬教诲,能不用gets就不用gets!!!

(⊙v⊙)嗯~ 代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5
 6 const int N = 1000001;
 7 int n,m,nxt[N],j;
 8 char s1[N],s2[N];
 9
10 void INT() {
11     nxt[0]=0,nxt[1]=0;
12     for(int i=1; i<m; i++) {
13         j = nxt[i];
14         while(j>0 && s2[i]!=s2[j]) j=nxt[j];
15         if(s2[i]==s2[j]) nxt[i+1]=j+1;
16         else nxt[i+1] = 0;
17     }
18 }
19
20 void find() {
21     j=0;
22     for(int i=0; i<n; i++) {
23         while(j>0&&s1[i]!=s2[j])  j=nxt[j];
24         if(s1[i]==s2[j]) j++;
25         if(j == m)
26             cout<<i-m+2<<endl;
27     }
28 }
29
30 int main() {
31     /*gets(s1);
32      gets(s2);*/ //无情的删去
33      cin>>s1>>s2;
34     n=strlen(s1); m=strlen(s2);
35     INT();
36     find();
37     for(int i=1; i<=m; i++)
38         cout<<nxt[i]<<" ";
39     return 0;
40 }

2. POJ 3461 Oulipo

Oulipo

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 40182   Accepted: 16143

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

Source

BAPC 2006 Qualification

代码几乎和上一个题一样,因为此题是在②号子串里找①号子串,所以输入的时候倒一下顺序,不要用gets输入。

(⊙v⊙)嗯~ 代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 using namespace std;
 5 char t[1000001],p[1000001];
 6 int lt,lp,f[1000001],ans,s;
 7 void INT()
 8 {
 9     f[1]=0;
10     for(int i=1;i<lp;i++)
11     {
12         int j=f[i];
13         while(j&&p[i]!=p[j]) j=f[j];
14         f[i+1]= p[i]==p[j] ? j+1 : 0;
15     }
16 }
17 void Find()
18 {
19     int j=0;
20     for(int i=0;i<lt;i++)
21     {
22         while(j&&(p[j]!=t[i])) j=f[j];
23         if(p[j]==t[i]) j++;
24         if(j==lp) ans++;
25     }
26 }
27 int main()
28 {
29     cin>>s;
30     while(s--) {
31         ans=0;
32         memset(f,0,sizeof(f));
33         cin>>p>>t;
34         lt=strlen(t);
35         lp=strlen(p);
36         INT();
37         Find();
38         cout<<ans<<endl;
39     }
40     return 0;
41 }

自己选的路,跪着也要走完!

时间: 2024-08-25 00:39:45

KMP——模板 练手题的相关文章

KMP算法的定义及KMP练手题 HDU 1711 Number Sequence (我的模板代码)

题意:就是要你来找出b数组在a数组中最先匹配的位置,如果没有则输出-1 思路:直接KMP算法(算法具体思想这位牛写的不错http://blog.csdn.net/v_july_v/article/details/7041827) AC代码: #include<cstdio> #include<cstring> #include<stdlib.h> #include<iostream> using namespace std; #define maxn 100

练手题,没事就来AC吧 poj 4044 Score Sequence

此题为12年金华邀请赛A题 克隆了下比赛,A题最简单,也是最挑战人数据处理能力的一题,可惜自己数据处理能力太弱 久久不能写出代码---- 总结下就是题做少了,平时应多做题,少灌水,应放下看电影的时间,玩各种软件的时间 先做好一项再说才是正道,看到一句话说得好 "   人有两条路要走,一条是必须走的,一条是想走的,你必须把必须走的路走漂亮,才可以走想走的路..." 不扯了,贴代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

【背包问题】【出来混总是要还的...】总结+入门练手题

花了一晚上加一早上研究背包,唉一大把年纪了才狠下心弄dp也确实说不过去的...... 背包入门当然还是看背包九讲(链接很多,没找到原作的,就随便贴一个链接了...),我再扯也是班门弄斧,只是贴一些摘要以及写代码时候的总结吧. 01背包:有N件物品和一个容量为V的背包.第i件物品的体积是v[i],价值是val[i],每种只有一件.求解将哪些物品装入背包可使价值总和max_val最大. max_val[i][j] -->  dp[i][j] : 从前i个物品中选择重量不超过j的物品时的最大价值: m

day-1.python初学者练手题

1.编写一个名为right_justify的函数,函数接受一个名为``s``的字符串作为形参, 并在打印足够多的前导空格(leading space)之后打印这个字符串,使得字符串的最后一个字母位于显示屏的第70列. def right_justify(s): length = len(s) lspace = 70 - length so = ' ' * lspace + s print(so) right_justify('dean') 2.函数对象是一个可以赋值给变量的值,也可以作为实参传递

NOI 练手题 图像旋转翻转变换

题目:来源http://noi.openjudge.cn/ch0112/09/ 总时间限制:  1000ms 内存限制:  65536kB 描述 给定m行n列的图像各像素点灰度值,对其依次进行一系列操作后,求最终图像. 其中,可能的操作及对应字符有如下四种: A:顺时针旋转90度: B:逆时针旋转90度: C:左右翻转: D:上下翻转. 输入 第一行包含两个正整数m和n,表示图像的行数和列数,中间用单个空格隔开.1 <= m <= 100, 1 <= n <= 100.接下来m行,

Pandas数据分析练手题(十题)

数据集下载地址:https://github.com/Rango-2017/Pandas_exercises ---------------------------------------------------------------------------------------------------------------------- 1 - 开始了解你的数据 探索Chipotle快餐数据 -- 将数据集存入一个名为chipo的数据框内-- 查看前10行内容-- 数据集中有多少个列(c

java 水题练手汇总

最近学java,会陆续找点水题练手. 题目链接 1 import java.util.*; 2 import java.awt.*; 3 import java.math.*; 4 5 public class Main { 6 7 public static void main(String args[]) { 8 Scanner cin=new Scanner(System.in); 9 int n, i; 10 int f[] = new int[35]; 11 f[0] = 0; 12

POJ Oulipo(KMP模板题)

题意:找出模板在文本串中出现的次数 思路:KMP模板题 #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #include<vector> #include<map> #include<queue> #include<stack&

MemSQL Start[c]UP 2.0 - Round 1(无聊练手B题)

http://codeforces.com/contest/452/problem/B B. 4-point polyline time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a rectangular grid of lattice points from (0, 0) to (n, m) inc