↓ Skip to main content
  1. Posts/

hdu 1358 Period (kmp,求字符串的周期)

·582 words·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

hdu 1358 题目链接

题意:给一个字符串,求这个字符串的每个前缀(包括本身)的能否由k个子串组成(K>1)

思路:和poj 2406 比较类似。。

结论:字符编号从0开始,如果又i%(i-next[i])==0,则i前面的 串为一个轮回串,其中轮回子串出现i/(i-next[i])次。

证明类似之前最小覆盖中的,不断的等价交换,两两相等…(这个证明在字符串这里总是遇到2333)

然而其实我。。。做的时候。。并没有想到去证明。。。而是打印出了nxt数组然后找规律求得2333.

之前一直觉得找规律不是什么拿的上台面的做法。。。但是今年打了几场多校。。尤其是电子科大的那场。。。我发现。。。其实有的题目的正解就是找规律,猜结论。。。

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年08月11日 星期四 03时20分44秒
 4File Name :code/hdu/1358.cpp
 5************************************************ */
 6#include <cstdio>
 7#include <cstring>
 8#include <iostream>
 9#include <algorithm>
10#include <vector>
11#include <queue>
12#include <stack>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <deque>
19#include <ctime>
20#define fst first
21#define sec second
22#define lson l,m,rt<<1
23#define rson m+1,r,rt<<1|1
24#define ms(a,x) memset(a,x,sizeof(a))
25typedef long long LL;
26#define pi pair < int ,int >
27#define MP make_pair
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=1E6+7;
34int nxt[N];
35char a[N];
36int n ;
37void getnxt( char *s)
38{
39    int i = 0 ;
40    int j = -1;
41    nxt[0] = -1;
42    int n = strlen(s);
43    while (i<n)
44	if (j==-1||s[i]==a[j]) nxt[++i]=++j;
45	else j = nxt[j];
46}
47int main()
48{
49	#ifndef  ONLINE_JUDGE
50	freopen("code/in.txt","r",stdin);
51  #endif
52	int cas = 0 ;
53	while (~scanf("%d",&n))
54	{
55	    if (n==0) break;
56	    printf("Test case #%d\n",++cas);
57	    scanf("%s",a);
58	    getnxt(a);
59//	    for ( int i = 0 ; i <= n ; i++  ) printf("nxt[%d]:%d\n",i,nxt[i]);
60	    for ( int i = 1 ; i <= n ; i++)
61	    {
62		if (nxt[i]==0) continue; //因为题目要求k>1
63		int tmp = i-nxt[i];
64		if (i%tmp==0)
65		    printf("%d %d\n",i,i/tmp);
66	    }
67	    printf("\n");
68	}
69  #ifndef ONLINE_JUDGE
70  fclose(stdin);
71  #endif
72    return 0;
73}

Related

hdu 2087 剪花布条 (kmp,不允许重叠的匹配)

·343 words·1 min
hdu 2087 题目链接 题意:问模式串在文本串中出现的次数,不允许重叠。 思路:kmp,关键在于不允许重叠。。。 其实只要每次找到的时候j=0一下就好咯。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年08月11日 星期四 02时52分44秒 4File Name :code/hdu/2087.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <stack> 14#include <set> 15#include <map> 16#include <string> 17#include <cmath> 18#include <cstdlib> 19#include <deque> 20#include <ctime> 21#define fst first 22#define sec second 23#define lson l,m,rt<<1 24#define rson m+1,r,rt<<1|1 25#define ms(a,x) memset(a,x,sizeof(a)) 26typedef long long LL; 27#define pi pair < int ,int > 28#define MP make_pair 29 30using namespace std; 31const double eps = 1E-8; 32const int dx4[4]={1,0,0,-1}; 33const int dy4[4]={0,-1,1,0}; 34const int inf = 0x3f3f3f3f; 35const int N=1E3+7; 36char a[N],b[N]; 37int nxt[N]; 38 39void getnxt(char *s) 40{ 41 int i = 0; 42 int j = -1; 43 nxt[0] = -1; 44 int n = strlen(s); 45 while (i<n) 46 if (j==-1||s[i]==s[j]) nxt[++i]=++j; 47 else j = nxt[j]; 48} 49int kmp(char *a,char *b) 50{ 51 int n = strlen(a); 52 int m = strlen(b); 53 getnxt(b); 54 int i = 0; 55 int j = 0; 56 int cnt = 0 ; 57 while (i<n) 58 { 59 if (j==-1||a[i]==b[j]) i++,j++; 60 else j=nxt[j]; 61 if (j==m) 62 { 63 cnt++; 64 j=0; //不允许重叠 65 } 66 } 67 return cnt; 68 69} 70int main() 71{ 72 #ifndef ONLINE_JUDGE 73 freopen("code/in.txt","r",stdin); 74 #endif 75 76 while (~scanf("%s",a)) 77 { 78 if (a[0]=='#') break; 79 scanf("%s",b); 80 int ans = kmp(a,b); 81 printf("%d\n",ans); 82 } 83 84 #ifndef ONLINE_JUDGE 85 fclose(stdin); 86 #endif 87 return 0; 88}

hdu 1711 Number Sequence (kmp)

·356 words·1 min
hdu 1711 题目链接 题意:给定两个数列,问第二个数列在第一个数列中出现的位置(第一个元素对应的位置) 思路:数列也可以看成字符串,然后左kmp,返回的答案是i+1-m。。。1A

KMP与最小覆盖子串

·1041 words·3 mins
参考资料(本文大部分是参考这篇博客,附带一些证明步骤的解释) 首先明确一些概念: 最小覆盖子串:对于某个字符串s,它的最小覆盖子串指的是长度最小的子串p,p满足通过自身的多次连接得到q,最后能够使s成为q的子串。 比如: 对于s=“abcab”,它的最小覆盖子串p=“abc”,因为p通过在它后面再接上一个p(即重叠0个字符),可以得到q=“abcabc”,此时s是q的子串。 对于s=“ababab”,它的最小覆盖子串为p=“ab”。