↓ Skip to main content
  1. Posts/

最小表示法学习笔记(同构问题+模板)

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

首先放一波资料:

参考博客 对于字符串循环同构的最小表示法,其问题实质是求S串的一个位置,从这个位置开始循环输出S,得到的S’字典序最小。

一种朴素的方法是设计i,j两个指针。其中i指向最小表示的位置,j作为比较指针。

令i=0,j=1 如果S[i] > S[j] i=j, j=i+1 如果S[i] < S[j] j++ 如果S[i]==S[j] 设指针k,分别从i和j位置向下比较,直到S[i] != S[j] _ __如果S[i+k] > S[j+k] i=j,j=i+1 _ 否则j++ 返回i

注意到,朴素算法的缺陷在于斜体的情况下i指针的移动太少了。针对这一问题改进就得到了最小表示法的算法。最小表示法的算法思路是维护两个指针i,j。

令i=0,j=1 如果S[i] > S[j] i=j, j=i+1 如果S[i] < S[j] j++ 如果S[i]==S[j] 设指针k,分别从i和j位置向下比较,直到S[i] != S[j] **如果S[i+k] > S[j+k] i=i+k ** 否则j++ 返回i和j的小者

注意到上面两个算法唯一的区别是粗体的一行。这一行就把复杂度降到O(n)了。 值得一提的是,与KMP类似,最小表示法处理的是一个字符串S的性质,而不是看论文时给人感觉的处理两个字符串。 应用最小表示法判断两个字符串同构,只要将两个串的最小表示求出来,然后从最小表示开始比较。剩下的工作就不用多说了。

模板:

 1#include <stdio.h>
 2#include <string.h>
 3const int N = 100000+10;
 4char str[N];
 5int minimalRepresentation()
 6{
 7    int n = strlen(str);
 8    int i = 0,j = 1, k = 0;
 9    while(i<n && j<n && k<n)
10    {
11        int t = str[(i+k)%n] - str[(j+k)%n] ;
12        if(t == 0)
13            k++;
14        else
15        {
16            if(t>0)
17                i+=k+1;
18            else
19                j+=k+1;
20            if(i==j)
21                j++;
22            k = 0;
23        }
24    }
25    return i < j ? i : j;
26}
27int main()
28{
29    int t;
30    scanf("%d",&t);
31    int n;
32    while(t--)
33    {
34        scanf("%d",&n);
35        scanf("%s",str);
36        int index = minimalRepresentation();
37        printf("%d\n",index);
38    }
39}

Related

hdu 4300 Clairewd’s message (kmp)

·721 words·2 mins
hdu 4300题目链接 吐槽:题意难懂的一逼,关键的地方根本没有说清好么。。。竟然还是多校题。。。。出题人英语是体育老师教的吧。。?本来挺傻逼一道题。。被这完全没有说清楚的题意搞得很不爽。。。

hdu 2594 Simpsons’ Hidden Talent (kmp)

·310 words·1 min
hdu 2594 题目链接 题意:given string s1,s2, find the longest prefix of s1 that is a suffix of s2. 思路:kmp。。。懒得说了。注意边界。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年08月12日 星期五 01时12分51秒 4File Name :code/hdu/2594.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=5E5+7; 34char a[N],b[N]; 35int nxt[N]; 36void getnxt( char *s) 37{ 38 int n = strlen(s); 39 int i = 0 ; 40 int j = -1; 41 nxt[0] = -1; 42 while (i<n) 43 if (j==-1||s[i]==s[j]) nxt[++i]=++j; 44 else j = nxt[j]; 45} 46int kmp(char *a,char *b) 47{ 48 int n = strlen(a); 49 int m = strlen(b); 50 getnxt(b); 51 int i = 0; 52 int j = 0; 53 while (i<n) 54 { 55 if (j==-1||a[i]==b[j]) i++,j++; 56 else j = nxt[j]; 57 } 58 if (i==n) return j; 59 return 0; 60} 61int main() 62{ 63 #ifndef ONLINE_JUDGE 64 freopen("code/in.txt","r",stdin); 65 #endif 66 while (~scanf("%s %s",a,b)) 67 { 68// cout<<"a:"<<a<<" b:"<<b<<endl; 69 int k = kmp(b,a); 70 if (k==0) 71 puts("0"); 72 else printf("%s %d\n",b+(strlen(b)-k),k); 73 74 ms(a,0); 75 ms(b,0); 76 } 77 #ifndef ONLINE_JUDGE 78 fclose(stdin); 79 #endif 80 return 0; 81}

hdu 2203 亲和串 (kmp)

·445 words·1 min
hdu 2203 题目链接 题意:给定字符串A(一个环),和字符串B,问B是否在A中出现过。 思路:环的问题。。复制一遍到末尾就好了。。出于严谨的考虑。。我们只复制n-1个字符。 以及要记得判断文本串的长度是否大于等于模板串,如果小于,直接判断no//然而题目数据太水,没判也过了

hdu 3746 Cyclic Nacklace (最小覆盖子串,kmp)

·380 words·1 min
hdu 3746题目链接 题意:给定一个字符串,是一个环(首尾相连),问至少再添加多少个珠子才能使得整个串是循环的。。。 思路:一下子想到了最小覆盖子串的模型。。。我求出最小覆盖子串的长度(n-nxt[n])。然后特判下最小覆盖子串的长度等于字符串长度的情况。。。试着叫了一发。。。竟然就A了2333.。。大概是所谓的题感吧(逃