剪花布条(KMP)HDU-2087
作者:互联网
一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案。对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢?
入门题:直接套模板就好,不会KMP的同学上网随便找一篇大佬的博客就可以了(自己写起来太麻烦了)
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<cstring> #include<stdio.h> #include<algorithm> #include<map> #include<queue> #include<set> #include <sstream> #include<vector> #include<cmath> #include<stack> #include<time.h> #include<ctime> using namespace std; #define inf 1<<30 #define eps 1e-7 #define LD long double #define LL long long #define maxn 1000005 int Next[maxn] = {}; void GetNext(string s)//找到Next数组 { memset(Next, 0, sizeof(Next)); int i = 0; int j = -1; int len = s.length(); Next[0] = -1; while (i < len) { if (j == -1 || s[i] == s[j]) { i++; j++; Next[i] = j; } else { j = Next[j]; } } } int Kmp(string p, string s) { GetNext(s); int i = 0; int j = 0; int len1 = p.length(); int len2 = s.length(); int sum = 0; while (i < len1) { if (j == -1 || p[i] == s[j]) { i++; j++; } else { j = Next[j]; } if (j == len2)//找到一个 { sum++;//加一 j =0;//j清零 } } return sum; } int main() { string p, s; while (cin >> p) { if (p == "#")break; cin >> s; printf("%d\n", Kmp(p, s)); } }
标签:HDU,布条,剪花,2087,图案,小饰条,KMP,include,define 来源: https://www.cnblogs.com/whhh/p/13504061.html