GYM 100883 F.Print Mix Strings(枚举)

253 篇文章 2 订阅

Description
给出两个串a,b,要求在不改变两个串内部的先后顺序的条件下把两个串并在一起,输出所有情况
Input
第一行一整数T表示用例组数,每组用例输入两个长度不超过8的串(1<=T<=100
Output
对于每组用例,以字典序升序不重复的输出所有情况,每组输出后跟一空行
Sample Input
2
a aa
ab cd
Sample Output
aaa

abcd
acbd
acdb
cabd
cadb
cdab

Solution
假设a和b串长分别为n和m,2^(n+m)枚举状态,1位置放a串,0位置放b串,之后拿map或set存起来去个重即可
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值