Unshuffle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 78 Accepted Submission(s): 15
Special Judge
Problem Description
A shuffle of two strings is formed by interspersing the characters into a new string, keeping the characters of each string in order. For example, MISSISSIPPI is a shuffle of MISIPP and SSISI. Let me call a string square if it is a shuffle of two identical strings. For example, ABCABDCD is square, because it is a shuffle of ABCD and ABCD, but the string ABCDDCBA is not square.
Given a square string, in which each character occurs no more than four times, unshuffle it into two identical strings.
Given a square string, in which each character occurs no more than four times, unshuffle it into two identical strings.
Input
First line, number of test cases, T.
Following are 2*T lines. For every two lines, the first line is n, length of the square string; the second line is the string. Each character is a positive integer no larger than n.
T<=10, n<=2000.
Following are 2*T lines. For every two lines, the first line is n, length of the square string; the second line is the string. Each character is a positive integer no larger than n.
T<=10, n<=2000.
Output
T lines. Each line is a string of length n of the corresponding test case. '0' means this character belongs to the first string, while '1' means this character belongs to the second string. If there are multiple answers, output any one of them.
Sample Input
1 8 1 2 3 1 2 4 3 4
Sample Output
00011011
Source
Recommend
zhuyuanchen520
简单题。。暴搜一遍就行了……
因为重复的不超过4个,dfs剪剪应该应该能过。。。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int a[2005];
int num[2005];
int first[2005];
int second[2005];
int ans[2005];
int n,ok;
int firstuse[2005];
int seconduse[2005];
void DFS(int t,int p,int q)
{
// printf("%d %d %d\n",t,p,q);
if (t==n)
{
ok=1;
return;
}
if (ok==1) return;
if (p==q)
{
if (firstuse[a[t]]<num[a[t]]/2 && p<n/2)
{
firstuse[a[t]]++;
first[p]=a[t];
ans[t]=0;
DFS(t+1,p+1,q);
if (ok==1) return;
firstuse[a[t]]--;
}
if (seconduse[a[t]]<num[a[t]]/2 && q<n/2)
{
seconduse[a[t]]++;
second[q]=a[t];
ans[t]=1;
DFS(t+1,p,q+1);
if (ok==1) return;
seconduse[a[t]]--;
}
}
if (p<q)
{
if (second[p]==a[t])
{
if (firstuse[a[t]]<num[a[t]]/2 && p<n/2)
{
firstuse[a[t]]++;
first[p]=a[t];
ans[t]=0;
DFS(t+1,p+1,q);
if (ok==1) return;
firstuse[a[t]]--;
}
}
if (seconduse[a[t]]<num[a[t]]/2 && q<n/2)
{
seconduse[a[t]]++;
second[q]=a[t];
ans[t]=1;
DFS(t+1,p,q+1);
if (ok==1) return;
seconduse[a[t]]--;
}
}
if (p>q)
{
if (first[q]==a[t])
{
if (seconduse[a[t]]<num[a[t]]/2 && q<n/2)
{
seconduse[a[t]]++;
second[q]=a[t];
ans[t]=1;
DFS(t+1,p,q+1);
if (ok==1) return;
seconduse[a[t]]--;
}
}
if (firstuse[a[t]]<num[a[t]]/2 && p<n/2)
{
firstuse[a[t]]++;
first[p]=a[t];
ans[t]=0;
DFS(t+1,p+1,q);
if (ok==1) return;
firstuse[a[t]]--;
}
}
}
int main()
{
int i,j,T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
memset(num,0,sizeof(num));
for (i=0;i<n;i++)
{
scanf("%d",&a[i]);
num[a[i]]++;
}
ok=0;
memset(firstuse,0,sizeof(firstuse));
memset(seconduse,0,sizeof(seconduse));
DFS(0,0,0);
for (i=0;i<n;i++)
{
printf("%d",ans[i]);
}
printf("\n");
}
return 0;
}