↓ Skip to main content
  1. Posts/

ural 1314 Hidden Password (字符串的最小表示法模板题)

·268 words·1 min
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

uva 1314 题目链接

题意:给定一个循环字符串,问字典序最小的串的开始位置。最小表示法裸题。

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年08月12日 星期五 18时48分29秒
 4File Name :code/uva/1314.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=1E5+7;
34int n ;
35char s[N];
36int minRep(char *s)
37{
38    int n = strlen(s);
39    int i = 0;
40    int j = 1;
41    int k = 0;
42    while (i<n&&j<n&&k<n)
43    {
44	int t = s[(i+k)%n]-s[(j+k)%n];
45	if (t==0) k++;
46	else
47	{
48	    if (t>0)
49		i+=k+1;
50	    else j+=k+1;
51	    if (i==j) j++;
52	    k = 0 ;
53	}
54    }
55    return i<j?i:j;
56}
57int main()
58{
59	#ifndef  ONLINE_JUDGE
60	freopen("code/in.txt","r",stdin);
61  #endif
62	int T;
63	cin>>T;
64	while (T--)
65	{
66	    scanf("%d\n%s",&n,s);
67	    printf("%d\n",minRep(s));
68	}
69  #ifndef ONLINE_JUDGE
70  fclose(stdin);
71  #endif
72    return 0;
73}

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//然而题目数据太水,没判也过了