E.Text Editor (Gym 101466E + 二分 + kmp)

时间:2024-08-06 20:33:38

题目链接:http://codeforces.com/gym/101466/problem/E

题目:

E.Text Editor (Gym 101466E + 二分 + kmp)

E.Text Editor (Gym 101466E + 二分 + kmp)

题意:

  给你s串和t串,一个数k,求t的最长前缀串在s串中出现次数不少于k。

思路:

  一眼二分+kmp,二分最长前缀串的长度即可。

代码实现如下:

 #include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;;
typedef pair<int, int> pii;
typedef unsigned long long ull; #define lson i<<1
#define rson i<<1|1
#define lowbit(x) x&(-x)
#define bug printf("*********\n");
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define FIN freopen("D://code//in.txt", "r", stdin);
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = 1e9 + ;
const int maxn = 1e5 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f; int k, lens1, lens2, ans, num;
char s1[maxn], s2[maxn];
int nex[maxn]; void get_next() {
nex[] = ;
for(int i = , j = ; i <= lens2; i++) {
while(j > && s2[i] != s2[j+]) j = nex[j];
if(s2[i] == s2[j+]) j++;
nex[i] = j;
}
} void kmp() {
get_next();
for(int i = , j = ; i <= lens1; i++) {
while(j > && (j == lens2 || s1[i] != s2[j+])) j = nex[j];
if(s1[i] == s2[j+]) j++;
if(j == lens2) {
num++;
}
}
} bool check(int x) {
lens2 = x;
num = ;
kmp();
return num >= k;
} int main() {
fgets(s1 + , maxn, stdin);
fgets(s2 + , maxn, stdin);
scanf("%d", &k);
lens1 = strlen(s1 + ), lens2 = strlen(s2 + );
int ub = lens2, lb = , mid;
ans = ;
while(ub >= lb) {
mid = (ub + lb) >> ;
if(check(mid)) {
ans = mid;
lb = mid + ;
} else {
ub = mid - ;
}
}
if(ans > ) {
for(int i = ; i <= ans; i++) {
printf("%c", s2[i]);
}
printf("\n");
}
else printf("IMPOSSIBLE\n");
return ;
}