KMP水题

HUST 1010 The Minimum Length

题目传送:The Minimum Length

AC代码:

#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;

char s[1000005];

int s_next[1000005];

int main() {
    while(scanf("%s", s) != EOF) {
        memset(s_next, 0, sizeof(s_next));
        s_next[0] = -1;
        int i = 0, j = -1;
        int len = strlen(s);
        while(i < len) {
            if(j == -1 || s[i] == s[j]) s_next[++ i] = ++ j;
            else j = s_next[j];
        }

        printf("%d\n", len - s_next[len]);  //即输出最小循环节
    }
    return 0;
}

POJ 2406 Power Strings

题目传送:Power Strings

AC代码:

#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;

char s[1000005];

int s_next[1000005];

int main() {
    while(scanf("%s", s) != EOF) {
        if(strcmp(s, ".") == 0) break;

        memset(s_next, 0, sizeof(s_next));
        s_next[0] = -1;

        int len = strlen(s);
        int i = 0, j = -1;
        while(i < len) {
            if(j == -1 || s[i] == s[j]) s_next[++ i] = ++ j;
            else j = s_next[j];
        }

        if(len % (len - s_next[len]) == 0)  //特判
            printf("%d\n", len / (len - s_next[len]));
        else printf("1\n");

    }
    return 0;
}

POJ 2752 Seek the Name, Seek the Fame

题目传送:Seek the Name, Seek the Fame

AC代码:

#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;

char s[400005];

int s_next[400005];

void fun(int len) {
    if(len <= 1) return;
    fun(s_next[len]);
    if(s_next[len] != 0)printf("%d ", s_next[len]);
}

int main() {
    while(scanf("%s", s) != EOF) {
        s_next[0] = -1;
        int len = strlen(s);
        int i = 0, j = -1;
        while(i < len) {
            if(j == -1 || s[i] == s[j]) s_next[++ i] = ++ j;
            else j = s_next[j];
        }

        fun(len);//递归打印 

        printf("%d\n", len);
    }
    return 0;
}

HDU 2594 Simpsons’ Hidden Talents

题目传送:Simpsons’ Hidden Talents

AC代码:

#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;

char s1[100005];
char s2[100005];

int s_next[100005];

int main() {
    while(scanf("%s %s", s1, s2) != EOF) {
        int len1 = strlen(s1);
        int len2 = strlen(s2);
        for(int i = len1, j = 0; j <= len2; i ++, j ++) {
            s1[i] = s2[j];
        }
//      printf("%s\n", s1);

        int len = len1 + len2;
        s_next[0] = -1;
        int i = 0, j = -1;
        int mi = min(len1, len2);
        while(i < len) {
            if((j == -1 || s1[i] == s1[j]) && j < mi) { //最大前缀后缀只能算小于mi的,大于等于mi就不对了,因为是比较两个字符串的前缀和后缀
                s_next[++ i] = ++ j;
            }
            else j = s_next[j];
        }

        int t = s_next[len];

        if(t > 0) {

            for(int i = 0; i < t; i ++) {
                printf("%c", s1[i]);
            }
            printf(" %d\n", t);
        }
        else printf("0\n");

    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-13 20:47:56

KMP水题的相关文章

hdu2203 KMP水题

两种方法     第一种是纯粹KMP #include<stdio.h> #include<string.h> #include<iostream> using namespace std; char str1[200010],str2[100010]; int next[100010]; int get() { next[0]=-1; int j=0; int k=-1; int len=strlen(str2); while(j<len-1) { if(k==

[hdu2087]kmp水题

题意:求模板串在文本串中出现的次数(位置无交叉).只需在找到的时候把模板串指针归0即可. 1 #pragma comment(linker, "/STACK:10240000,10240000") 2 3 #include <iostream> 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstdlib> 7 #include <cstring> 8 #incl

[hdu2594]kmp水题

题意:求最长的a的前缀同时满足是b的后缀,把a,b连在一起,kmp跑一下,迭代next直到长度小于等于a,b长度的最小值为止,即为答案. 1 #pragma comment(linker, "/STACK:10240000,10240000") 2 3 #include <iostream> 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstdlib> 7 #include

hdu1686 KMP水题

题意是给出文本串和模式串  玩模式串在文本串中出现多少次         把KMP稍稍改动下就ok了 #include<stdio.h> #include<string.h> #include<iostream> using namespace std; char str1[10010],str[1000100]; int next[10010]; int deal(int len) { next[0]=-1; int j=0; int k=-1; while(j<

hdu 2087 剪花布条 KMP水题。。

剪花布条 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10399    Accepted Submission(s): 6701 Problem Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input 输入中含

Poj--3080Blue Jeans+KMP水题

题目链接:点击进入 大概的题意就是给n个字符串,然后让我们找出他们的最长的公共子串. 因为题目的数据比较小,我们可以枚举第一个串的所有子串,然后再用KMP判断一下这个子串是否出现在其它字符串中. 代码如下: #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn=100; ///x是模式串,m是长度,next是模式串对应的next数组 void k

hdu1711 Number Sequence(KMP水题)

题意: 在a串中寻找第一个包含b串的的位置 思路:直接KMP即可 #include <cstdio> #define MAXN 1000010 using namespace std; void kmp_pre(int x[],int m,int next[]){ int i,j; j=next[0]=-1; i=0; while(i<m){ while(-1!=j&&x[i]!=x[j]) j=next[j]; if(x[++i]==x[++j]) next[i]=ne

刷了500道水题是什么体验?

并没有什么卵用. 我马上大二了,大一两学期目测切了1000道水题了,毫无意义. 至今不理解kmp和后缀数组,只会模板.数论和博弈论是什么?能吃吗?只会打表.图论至今不会tarjan,话说dlx是什么?插头dp,这是什么?数据结构还好,经常做高中生的题,可持久化可持久化线段树也能花一下午时间写出来,然而并不会考. 平时做题只刷水题,遇到难题的时候,随手搜题解,看了看,哇,这居然能这么搞!然后抄一遍别人代码,交上去ac. cf一年几乎没缺过,花了大一上半年时间才滚上div1.然而至今紫号一堆,黄名一

2015南阳CCPC L - Huatuo&#39;s Medicine 水题

L - Huatuo's Medicine Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Huatuo was a famous doctor. He use identical bottles to carry the medicine. There are different types of medicine. Huatuo put medicines into the bottles and chain these b