[找规律][kmp]Camp Schedule Codeforces1137B

The new camp by widely-known over the country Spring Programming Camp is going to start soon. Hence, all the team of friendly curators and teachers started composing the camp's schedule. After some continuous discussion, they came up with a schedule ss, which can be represented as a binary string, in which the ii-th symbol is '1' if students will write the contest in the ii-th day and '0' if they will have a day off.

At the last moment Gleb said that the camp will be the most productive if it runs with the schedule tt (which can be described in the same format as schedule ss). Since the number of days in the current may be different from number of days in schedule tt, Gleb required that the camp's schedule must be altered so that the number of occurrences of tt in it as a substring is maximum possible. At the same time, the number of contest days and days off shouldn't change, only their order may change.

Could you rearrange the schedule in the best possible way?

Input

The first line contains string ss (1⩽|s|⩽5000001⩽|s|⩽500000), denoting the current project of the camp's schedule.

The second line contains string tt (1⩽|t|⩽5000001⩽|t|⩽500000), denoting the optimal schedule according to Gleb.

Strings ss and tt contain characters '0' and '1' only.

Output

In the only line print the schedule having the largest number of substrings equal to tt. Printed schedule should consist of characters '0' and '1' only and the number of zeros should be equal to the number of zeros in ss and the number of ones should be equal to the number of ones in ss.

In case there multiple optimal schedules, print any of them.

Examples

input

101101
110

output

110110

input

10010110
100011

output

01100011

input

10
11100

output

01

Note

In the first example there are two occurrences, one starting from first position and one starting from fourth position.

In the second example there is only one occurrence, which starts from third position. Note, that the answer is not unique. For example, if we move the first day (which is a day off) to the last position, the number of occurrences of tt wouldn't change.

In the third example it's impossible to make even a single occurrence.

题意: 给出两个01串,改变第一个串中01顺序使得第二个串在第一个串中出现次数最多。

分析: 对于第一个串只需要记录下其中01个数,之后就不需要用到它了。题目转化为用规定个数的0和1组成一个字符串,使给定的字符串在这个字符串中出现次数最多。可以发现,最优情况是这次出现的字符串后缀作为下一次出现的前缀,即最长前后缀匹配。很自然的想到需要利用kmp中的next数组含义。于是可以这样输出:先把整个串输出一遍,之后不停输出除最长前缀以外的部分,并维护现有数字个数,在这个过程中如果某个数字用完了之后就可以全部输出另一个数字。

具体代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
//贪心地想,在能够输出的前提下,应该连续输出p串除前缀以外的部分,这样才能使其出现次数最多
//当0或1用完时,全部输出另一个数字即可 
//010|0|010 0010 0010 0010... 
char s[500005], p[500005];
int _nextp[500005], num[2];

signed main()
{
	scanf("%s%s", s+1, p+1);
	int lens = strlen(s+1), lenp = strlen(p+1);
	for(int i = 1; i <= lens; i++)//分别记录01数量 
		 if(s[i] == '1')
		 	num[1]++;
		else
			num[0]++;
	for(int i = 2, j = 0; i <= lenp; i++)
	{
		while(j && p[j+1] != p[i])
			j = _nextp[j];
		if(p[j+1] == p[i])
			j++;
		_nextp[i] = j;
	}
	int t = lenp-_nextp[lenp];//除前缀以外部分长度 
	for(int i = 1; i <= _nextp[lenp]; i++)//先输出前缀 
	{
		if(num[p[i]-'0'])
			putchar(p[i]), num[p[i]-'0']--;
		else
			putchar(!(p[i]-'0')+'0');
	}
	for(int i = _nextp[lenp]+1, j = 0; i <= lens; i++, j++)//之后不停输出前缀以外部分 
	{
		int k = j%t+_nextp[lenp]+1;
		if(num[p[k]-'0'])
			putchar(p[k]), num[p[k]-'0']--;
		else
			putchar(!(p[k]-'0')+'0');
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值