kmp 最小循环节

Cyclic Nacklace HDU - 3746

题意:
T次询问,每次询问给你一个字符串s(|s| <= 1e5 ),问你在串尾最少加几个字符可以使s至少包含两个循环节。

思路:

  • next数组求循环节,s的最小循环节长度 m = strlen(s) - nxt[strlen(s)]。

  • 显然,s中没有出现最小循环节,那最小循环节就是s串本身(nxt[strlen(s)] = 0),若有则可以把后面完整或部分的循环节通过减nxt[strlen(s)]去掉,保留第一个。

code:



#include<iostream>
#include<cstdio> 
#include<cstring>
using namespace std;
const int maxn = 1e5 + 5; 
char s[maxn];
int nxt[maxn];

void get_nxt(int m){
	int i = 0, j = -1;
	nxt[0] = -1;
	while(i < m){
		if(j == -1 || s[i] == s[j]) nxt[++i] = ++j;
		else j = nxt[j]; 
	}
}
int main(){
	int T;
	scanf("%d", &T);
	while(T--){
		scanf("%s", s);
		int len = strlen(s);
		
		get_nxt(len);
	    
	    int m = len - nxt[len];
	    if(len != m && len % m == 0) printf("0\n");
	    else printf("%d\n", m - len % m);
		
	}
}

 

Period HDU - 1358

题意:
多组样例,给你一个字符串,输出它所有前缀中只包含完整的最小循环节、且最小循环节数量大于1的前缀尾位 i 和最小循环节数量 k 。

思路:
nxt数组求最小循环节,len为前缀的长度 ,若 len %(len - nxt[i])= 0 && len != len - nxt[i],则符合题意。

code:


#include<ostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
const int maxn = 1e6 + 5;
char s[maxn];
int nxt[maxn];
void get_nxt(int m)
{
	int i = 0, j = -1;	
	nxt[0]=-1;
	while(i < m)
	{
	    if(j == -1 || s[i] == s[j]) nxt[++i] = ++j;
	    else j = nxt[j];
	}
}
int main()
{
	int num, i;
	int cs=1;
	
	while(scanf("%d",&num)&&num)
	{
	    scanf("%s",s);
	    int len = strlen(s);
	    get_nxt(len);
	    printf("Test case #%d\n",cs++);
	
	    for(i = 2; i <= len; i++)
	    {
	        if(i % (i - nxt[i]) == 0 && i / (i - nxt[i]) > 1)
	            printf("%d %d\n",i, i / (i - nxt[i]));
	    }
	
	    printf("\n");
	}
	
	
}

The Minimum Length HUST - 1010

题意:
多组输入,求最小循环节的长度。

思路:
nxt数组求最小循环节,len - nxt[len]就是最小循环节的长度,len是串s的长度。

code:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 1e6 + 5;
char s[maxn];
int nxt[maxn];

void get_nxt(int m){
	int i = 0, j = -1;
	nxt[0] = -1;
	while(i < m){
		if(j == -1 || s[i] == s[j]) nxt[++i] = ++j;
		else j = nxt[j];
	}
}

int main(){
	while(~scanf("%s", s)){
		int len = strlen(s);
		get_nxt(len);
		printf("%d\n", len - nxt[len]);
	}
	
} 

Power Strings POJ - 2406

题意:
多组输入,’.’ 终止。求字符串的最小循环节的数量。

思路:
nxt数组求最小循环节。len = strlen(s)。当 len % (len - nxt[len]) = 0时,最小循环节的数量 len / (len - nxt[len]),否则为1。

code:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 1e6 + 5;
char s[maxn];
int nxt[maxn];

void get_nxt(int m){
	int i = 0, j = -1;
	nxt[0] = -1;
	while(i < m){
		if(j == -1 || s[i] == s[j]) nxt[++i] = ++j;
		else j = nxt[j];
	}
}
int main(){
	while(scanf("%s", s), s[0] != '.'){
		int len = strlen(s);
		get_nxt(len);
		if(len % (len - nxt[len]) == 0) printf("%d\n", len / (len - nxt[len])); 
		else printf("1\n");
	}
}

Seek the Name, Seek the Fame POJ - 2752

题意:
多次询问,每次给你一个字符串,找出即是前缀也是后缀的字串数量。

思路:
nxt[i] 代表以i结尾的非前缀子串(即后缀但不是全部字符)与 A的前缀能够匹配的最大长度 。即后缀与前缀的匹配情况。


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> 
using namespace std;
const int maxn = 4e5 + 5;
char s[maxn];
int nxt[maxn];
int ans[maxn];
void get_nxt(int m){
	int i = 0, j = -1;
	nxt[0] = -1;
	while(i < m){
		if(j == -1 || s[i] == s[j]) nxt[++i] = ++j;
		else j = nxt[j];
	}
}
int main(){

	while(~scanf("%s", s)){
		int len = strlen(s);
		get_nxt(len);
		
		int cot = 0;		
		ans[cot++] = len;
		while(nxt[len] > 0){
			ans[cot++] = nxt[len];
			len = nxt[len]; 
	
		}
		for(int i = cot - 1; i >= 0; i--) printf("%d ", ans[i]); printf("\n");	       		
		
	}
}

Simpsons’ Hidden Talents HDU - 2594

题意:
多次询问,每次给你两个字符串s、t(0 < |s|、|t| <= 5e4),求与s串前缀相同的t串最大长度后缀,输出最t串的最大后缀串,并输出最大后缀长度。

思路:
求出s串的nxt数组,s串作为模式串,t串作为文本串。t串匹配完,s串匹配到nxt[j]则t串的最大后缀为s[0 ~ j - 1]。

code:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 5e4 + 5;
char s[maxn], t[maxn];
int nxt[maxn];

void get_nxt(int m){
	int i = 0, j = -1;
	nxt[0] = -1;
 	while(i < m){
		if(j == -1 || s[i] == s[j]) nxt[++i] = ++j;
		else j = nxt[j];
	}
}

int main(){
	while(~scanf("%s%s", s, t)){
		
		int len1 = strlen(s);
		int len2 = strlen(t);
		get_nxt(len1);
		int i = -1, j = -1;
		while(i < len2){
			if(j == -1 || t[i] == s[j]){
				i++; j++;
			}
			else j = nxt[j];
		}
		
		if(j == 0) printf("0\n");
		else {
			for(int k = 0; k < j; k++) printf("%c", s[k]); 
			printf(" %d\n", j);		
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值