博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
字符串哈希+kmp题
阅读量:4350 次
发布时间:2019-06-07

本文共 6323 字,大约阅读时间需要 21 分钟。

9.7

Crazy Search(字符串哈希)

Many people like to solve hard puzzles some of which may lead them to madness. One such puzzle could be finding a hidden prime number in a given text. Such number could be the number of different substrings of a given size that exist in the text. As you soon will discover, you really need the help of a computer and a good algorithm to solve such a puzzle.
Your task is to write a program that given the size, N, of the substring, the number of different characters that may occur in the text, NC, and the text itself, determines the number of different substrings of size N that appear in the text.
As an example, consider N=3, NC=4 and the text "daababac". The different substrings of size 3 that can be found in this text are: "daa"; "aab"; "aba"; "bab"; "bac". Therefore, the answer should be 5.

Input

The first line of input consists of two numbers, N and NC, separated by exactly one space. This is followed by the text where the search takes place. You may assume that the maximum number of substrings formed by the possible set of characters does not exceed 16 Millions.

Output

The program should output just an integer corresponding to the number of different substrings of size N found in the given text.

Sample Input

3 4daababac

Sample Output

5 题意:就是给你n(字符长)m(字符种类),然后给你长的字符串,让你查找出以n为长度的不同的字符串有多少个
#include
#include
#include
using namespace std; const int maxn=16e6; bool hash[maxn]; char word[26]; int main() {
 int n,nc;  string s;  cin>>n>>nc>>s;  memset(hash,0,sizeof(hash));  memset(word,0,sizeof(word));  int k=0;    for(int i=0;i
 

 9.8

String  Problem(字符串最大最小表示)

Give you a string with length N, you can generate N strings by left shifts. For example let consider the string “SKYLONG”, we can generate seven strings:
String Rank
SKYLONG 1
KYLONGS 2
YLONGSK 3
LONGSKY 4
ONGSKYL 5
NGSKYLO 6
GSKYLON 7
and lexicographically first of them is GSKYLON, lexicographically last is YLONGSK, both of them appear only once.
  Your task is easy, calculate the lexicographically fisrt string’s Rank (if there are multiple answers, choose the smallest one), its times, lexicographically last string’s Rank (if there are multiple answers, choose the smallest one), and its times also.

Input  Each line contains one line the string S with length N (N <= 1000000) formed by lower case letters.OutputOutput four integers separated by one space, lexicographically fisrt string’s Rank (if there are multiple answers, choose the smallest one), the string’s times in the N generated strings, lexicographically last string’s Rank (if there are multiple answers, choose the smallest one), and its times also.Sample Input

abcderaaaaaaababab

Sample Output

1 1 6 11 6 1 61 3 2 3 题解:这道题就是输出所给字符串的最大最小表示的起始位置和数量! 这道题是kmp以及最大最小表示法的应用,数量我们可以用kmp中求循环节的函数,然后结合最大最小表示法模板
#include
using namespace std;const int maxn=1e6+10;char s[maxn];int ne[maxn];void kmpnext(char s[],int len){ int i=0,j=-1; ne[0]=-1; while(i
0)i=i+k+1; else j=j+k+1; } else { if(t>0)j=j+k+1; else i=i+k+1; } if(i==j) j++; k=0; } } if(i

 

9.9
For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the prefix is a periodic string. That is, for each i (2 <= i <= N) we want to know the largest K > 1 (if there is one) such that the prefix of S with length i can be written as A 
K ,that is A concatenated K times, for some string A. Of course, we also want to know the period K.
 
The input consists of several test cases. Each test case consists of two lines. The first one contains N (2 <= N <= 1 000 000) – the size of the string S.The second line contains the string S. The input file ends with a line, having the
number zero on it.
 
For each test case, output "Test case #" and the consecutive test case number on a single line; then, for each prefix with length i that has a period K > 1, output the prefix size i and the period K separated by a single space; the prefix sizes must be in increasing order. Print a blank line after each test case.
 
3aaa12aabaabaabaab0
 
Test case #12 23 3Test case #22 26 29 312 4 题解:这道题是kmp模板题,给定字符串,依次查找,判断有循环的位置以及循环个数,主要是用存循环节的函数
#include
using namespace std;const int maxn=1e6+10;int ne[maxn]; char s[maxn];void kmpnext(char s[],int n){ int i=0,j=-1; ne[0]=-1; while(i
1) cout<
<<" "<
<
>n&&n) { cin>>s; cout<<"Test case #"<
<

 

9。10
String matching is a common type of problem in computer science. One string matching problem is as following:
Given a string 
s[0len1]s[0…len−1], please calculate the length of the longest common prefix of s[ilen1]s[i…len−1] and s[0len1]s[0…len−1] for each i>0i>0.
I believe everyone can do it by brute force.
The pseudo code of the brute force approach is as the following:
We are wondering, for any given string, what is the number of compare operations invoked if we use the above algorithm. Please tell us the answer before we attempt to run this algorithm.
 
3
_Happy_New_Year_ywwywwzjczzzjczjczzzjc
 
17732 题意: 字符串匹配是计算机科学中常见的问题类型。一个字符串匹配问题如下:给定字符串s [0 ... len-1],请计算 每个i的s [i ... len-1]和s [0 ... len-1]的最长公共前缀的长度> 0。 我相信每个人都可以通过蛮力来做到这一点。蛮力方法的伪代码如下:我们想知道, 对于任何给定的字符串,如果我们使用上述算法,调用的比较操作的数量是多少。在我们尝试运行此算法之前,请告诉我们答案 这道题很懵,看了许多博客,也还是迷迷糊糊的的,扩展kmp,没搞懂,,,,
  • next[i]: T[i]...T[m - 1]与 T 的最长相同前缀长度;
  • extend[i]: S[i]...S[n - 1]与 T 的最长相同前缀长度。
 
#include
#include
#include
using namespace std;const int maxn=1e6+10;int Next[maxn];int expand[maxn];char s[maxn];void GNext(char t[],int le,int Next[]){ int a=0,b=0; Next[0]=le; for(int i=1;i
=b||i+Next[i-a]>=b) { if(i>=b) b=i; while(b
=b||i+Next[i-a]>=b) { if(i>=b) b=i; while(b
 

 总结:这周除了一些思维题,就是和kmp相关算法的题了,最大最小+kmp,扩展kmp,在学习过程中,也看了一些kmp讲解的视频,也搞懂了next存的前后缀,循环节,稍简单的还能做,但一和其他算法联结,就蒙圈了,看题解,但有些地方还是比较晦涩难懂,手动模拟,看视频,有些部分仍旧不能很好的连接!

 
 
 

转载于:https://www.cnblogs.com/ylrwj/p/11478327.html

你可能感兴趣的文章
使用Maven命令安装jar包到仓库中
查看>>
SQLite数据库
查看>>
[译]Vulkan教程(29)组合的Image采样器
查看>>
combox的DispalyMember和ValueMember属性的测试
查看>>
Start Developing Mac Apps -- Human Interface Design 用户界面设计
查看>>
linux下安装Mongodb
查看>>
Page.RegisterStartupScript和Response.Write的区别。
查看>>
hdu4348区间更新的主席树+标记永久化
查看>>
bzoj3261: 最大异或和 可持久化trie
查看>>
ZOJ 2532 Internship
查看>>
HDU 3452 Bonsai
查看>>
[Erlang12] Mnesia分布式应用
查看>>
图的遍历 | 1013 连通块块数
查看>>
Kinect 开发 —— 进阶指引(上)
查看>>
python学习笔记(六)time、datetime、hashlib模块
查看>>
uva489(需要考虑周全)
查看>>
C-关键字(二)
查看>>
排序笔记
查看>>
天气API整理,返回的数据格式为json对象
查看>>
andorid 字体 修改
查看>>