[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 <cstring>
  8 #include <map>
  9 #include <queue>
 10 #include <deque>
 11 #include <cmath>
 12 #include <vector>
 13 #include <ctime>
 14 #include <cctype>
 15 #include <set>
 16 #include <bitset>
 17 #include <functional>
 18 #include <numeric>
 19 #include <stdexcept>
 20 #include <utility>
 21
 22 using namespace std;
 23
 24 #define mem0(a) memset(a, 0, sizeof(a))
 25 #define mem_1(a) memset(a, -1, sizeof(a))
 26 #define lson l, m, rt << 1
 27 #define rson m + 1, r, rt << 1 | 1
 28 #define define_m int m = (l + r) >> 1
 29 #define rep_up0(a, b) for (int a = 0; a < (b); a++)
 30 #define rep_up1(a, b) for (int a = 1; a <= (b); a++)
 31 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
 32 #define rep_down1(a, b) for (int a = b; a > 0; a--)
 33 #define all(a) (a).begin(), (a).end()
 34 #define lowbit(x) ((x) & (-(x)))
 35 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
 36 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
 37 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
 38 #define pchr(a) putchar(a)
 39 #define pstr(a) printf("%s", a)
 40 #define sstr(a) scanf("%s", a)
 41 #define sint(a) scanf("%d", &a)
 42 #define sint2(a, b) scanf("%d%d", &a, &b)
 43 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
 44 #define pint(a) printf("%d\n", a)
 45 #define test_print1(a) cout << "var1 = " << a << endl
 46 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
 47 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
 48
 49 typedef double db;
 50 typedef long long LL;
 51 typedef pair<int, int> pii;
 52 typedef multiset<int> msi;
 53 typedef set<int> si;
 54 typedef vector<int> vi;
 55 typedef map<int, int> mii;
 56
 57 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
 58 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 };
 59 const int maxn = 2e5 + 7;
 60 const int md = 10007;
 61 const int inf = 1e9 + 7;
 62 const LL inf_L = 1e18 + 7;
 63 const double pi = acos(-1.0);
 64 const double eps = 1e-6;
 65
 66 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);}
 67 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
 68 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
 69 template<class T>T condition(bool f, T a, T b){return f?a:b;}
 70 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
 71 int make_id(int x, int y, int n) { return x * n + y; }
 72
 73 struct KMP {
 74     int next[1000010];
 75     void GetNext(char s[]) {
 76         mem0(next);
 77         next[0] = next[1] = 0;
 78         for(int i = 1; s[i]; i++) {
 79             int j = next[i];
 80             while(j && s[i] != s[j]) j = next[j];
 81             next[i + 1] = s[j] == s[i]? j + 1 : 0;
 82         }
 83     }
 84 };
 85
 86 KMP kmp;
 87 char s[maxn], s2[maxn];
 88
 89 int main() {
 90     //freopen("in.txt", "r", stdin);
 91     while (~scanf("%s%s", s, s2)) {
 92         int len = strlen(s), len2 = strlen(s2);
 93         rep_up0(i, len2) s[len + i] = s2[i];
 94         s[len + len2] = 0;
 95         kmp.GetNext(s);
 96         int pos = len + len2, minlen = min(len, len2);
 97         while (pos && pos > minlen) pos = kmp.next[pos];
 98         rep_up0(i, pos) pchr(s[i]);
 99         if (pos) pchr(‘ ‘);
100         printf("%d\n", pos);
101     }
102     return 0;
103 }

时间: 2025-01-31 03:59:39

[hdu2594]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

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

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 <s

刷了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