Description
Given two strings a and b, it is required that the two strings be put together and output all cases without changing the order of the two strings.
Input
The first line, an integer T, represents the number of use case groups, with each group of use cases entering two strings of no more than 8 lengths (1<=T<=100).
Output
For each set of use cases, the output is not repeated in dictionary ascending order. Each set of output is followed by a blank line.
Sample Input
2
a aa
ab cd
Sample Output
aaa
abcd
acbd
acdb
cabd
cadb
cdab
Solution
Assuming that the length of a and b strings is n and m respectively, 2^(n+m) enumeration state, a string is placed in 1 position, b string is placed in 0 position, and then map or set is saved to a weight.
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define maxn 1111111
string a,b,c;
char s[22];
int T,n,m;
map<string,int>M;
map<string,int>::iterator it;
string d[maxn];
int main()
{
scanf("%d",&T);
while(T--)
{
cin>>a>>b;
n=a.size(),m=b.size();
M.clear();
c.clear();
int N=1<<(n+m);
for(int i=0;i<N;i++)
{
int num1=0,num2=0;
for(int j=0;j<n+m;j++)
{
if(i&(1<<j))
{
if(num1<n)s[j]=a[num1++];
else
{
num1++;
break;
}
}
else
{
if(num2<m)s[j]=b[num2++];
else num2++;
}
}
if(num1!=n)continue;
s[n+m]='\0';
c=s;
M[c]=1;
}
int res=0;
for(it=M.begin();it!=M.end();it++)d[res++]=it->first;
sort(d,d+res);
for(int i=0;i<res;i++)cout<<d[i]<<endl;
cout<<endl;
}
return 0;
}