cf Text Editor(kmp + 二分 )

http://codeforces.com/gym/101466/problem/E

E. Text Editor

time limit per test

1.0 s

memory limit per test

512 MB

input

standard input

output

standard output

One of the most useful tools nowadays are text editors, their use is so important that the Unique Natural Advanced Language (UNAL) organization has studied many of the benefits working with them.

They are interested specifically in the feature "find", that option looks when a pattern occurs in a text, furthermore, it counts the number of times the pattern occurs in a text. The tool is so well designed that while writing each character of the pattern it updates the number of times that the corresponding prefix of the total pattern appears on the text.

Now the UNAL is working with the editor, finding patterns in some texts, however, they realize that many of the patterns appear just very few times in the corresponding texts, as they really want to see more number of appearances of the patterns in the texts, they put a lower bound on the minimum number of times the pattern should be found in the text and use only prefixes of the original pattern. On the other hand, the UNAL is very picky about language, so they will just use the largest non-empty prefix of the original pattern that fit into the bound.

Input

The first line contains the text A (1 ≤ |A| ≤  105) The second line contains the original pattern B (1 ≤ |B| ≤  |A|) The third line contains an integer n (1 ≤ n ≤  |A|) - the minimum number of times a pattern should be found on the text.

Output

A single line, with the prefix of the original pattern used by the UNAL, if there is no such prefix then print "IMPOSSIBLE" (without the quotes)

Examples

input

Copy

aaaaa
aaa
4

output

Copy

aa

input

Copy

programming
unal
1

output

Copy

IMPOSSIBLE

input

Copy

abracadabra
abra
1

output

Copy

abra

input

Copy

Hello World!
H W
5

output

Copy

IMPOSSIBLE

二分第二串的长度, 然后前缀求次数

#include<bits/stdc++.h>
using namespace std;
const int N = 3e7 + 5;
int _next[N], n;
string a, b, s;

void get_next(string T)//得到字符串T的_next数组值
{
	_next[0] = -1;
	int i = 0, j = -1;
	int len=T.length();
	while(i < len)
	{
		if(j == -1 || T[i] == T[j])
			_next[++i] = ++j;
		else
			j = _next[j];
	}
}
int kmp(string t, string p, int idx=0)//从idx开始匹配p在t中出现的位置
{
	int res = 0; 
	get_next(p);
	int i = idx, j = 0;
	int lent = t.length();
	int lenp = p.length(); 
	
	while (i < lent && j < lenp)
	{
		if (j == -1|| t[i] == p[j]) 	i++,j++;
	 	else 	j = _next[j];
	 	if( j == lenp)
	 	{
	 		i--, res++;
	 		j = _next[j-1];
		 }
	}
	
    return res;
}

int main()
{
	getline(cin,a);
	getline(cin,b);
	get_next(a);

	cin >> n;
	int l = 1, r = b.length();
	int mid = 0, ans = 0;
	while(l <= r)
	{
		mid = (l + r) >> 1;
		s= b.substr(0,mid);

		if(mid == 0) break;
		if(kmp(a, s, 0) >= n)
			ans = mid, l = mid + 1 ;
		else
			r = mid - 1;
	}

	if(ans == 0)
		cout << "IMPOSSIBLE" << endl;
	else
		cout << b.substr(0,ans) <<endl;

	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值