Camp Schedule CodeForces - 1138D (Next数组的变形应用)
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 110Output
110110Input
10010110 100011Output
01100011Input
10 11100Output
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.
题意:给你一个二进制串,然后给你一个模式串,你可以对主串的每个字符随意移动,使得模式串在主串中出现的次数最多
思路:求一下next数组,找出前缀与后缀的最大相同长度next[ptr_len],然后只需连续的输出前面一部分字符即可,直到1或者0的个数为零,跳出循环,把剩下的0或1输出
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
#define pb push_back
#define INF 999999999999999
const int maxn=1e6+5;
int mp[2],flag=0;
int next1[maxn];
void get_next(string a)
{
int len=a.size();
next1[0]=-1;
int j=0,k=-1;
while(j<len) {
if(k==-1||a[j]==a[k])
next1[++j]=++k;
else k=next1[k];
}
}
int main()
{
string s,t;
cin>>s>>t;
int ls=s.size(),lt=t.size();
for(int i=0;i<ls;i++)
mp[s[i]-'0']++;
get_next(t);
int len = lt - next1[lt];
for(int i=0;i<len;i++)
{
int x=t[i]-'0';
if(mp[x])
printf("%d",x),mp[x]--;
else
break;
if(i==len-1) i=-1;
}
while(mp[0])
printf("0"),mp[0]--;
while(mp[1])
printf("1"),mp[1]--;
return 0;
}
上一篇: Seek the Name, Seek the Fame
下一篇: 手把手带你开发豆瓣FM(vue)