AtCoder-C-String Coloring(字符串)

img

题意:

  • 输入 n n 和 字符串 s ,代表 字符串 s s 的长度为 2n
  • 正着选 n n 个字符构成一个字符串 s1 ,然后倒着选 n n 个字符构成一个字符串 s2 ,不能选重复的字符。

问:字符串 s1 s 1 与 字符串 s2 s 2 完全一样的的方案数是多少?

数据范围:

  • 1<=n<=18 1 <= n <= 18
  • 字符串 s s 都是小写字母

输入:

4
cabaacba

18
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

输出:

4

9075135300

[The answer may not be representable as a 32-bit integer.]

样例解释:

Therearefourwaystopaintthestring,asfollows:

  • [c] a b a [a] c [b] [a]
  • [c] a b [a] a c [b] [a]
  • c [a] [b] [a] a [c] b a
  • c [a] [b] a [a] [c] b a

解题思路:

因为字符串长度最大为36,那么我们可以先分成两半,然后二进制暴力枚举。0 代表倒着的,1 代表正着的。然后要相互对称。

AC代码:

#include<cstdio>
#include<algorithm>
#include<map>
#include<cstring>
#include<cmath>
#include<iostream>
using namespace std;
typedef long long LL;

map<pair <string,string>,LL> cnt;
const int maxn = 50;
char s[maxn];

int main(){
    int n; scanf("%d",&n);
    scanf("%s",s);
    for(int i = 0;i < (1 << n);i++){ // 先枚举左半边
        string s1 = "",s2 = "";
        for(int j = 0;j < n;j++){
            if(i & (1 << j)) s1 += s[j]; // 若 i 的第 j 位为 1,则 s[j] 加入 s1.
            else s2 += s[j]; // 若 i 的第 j 位为 0,则 s[j] 加入 s2.
        }
        cnt[make_pair(s1,s2)]++; // 开个 map 来记录有(s1,s2)有多少对
    }
    LL sum = 0;
    for(int i = 0;i < (1 << n);i++){ // 枚举右半边
        string s1 = "",s2 = "";
        for(int j = n - 1;j >= 0;j--){
            if(i & (1 << j)) s2 += s[j + n]; // 若 i 的第 j 位为 1,则 s[j + n] 加入 s2.
            else s1 += s[j + n]; // 若 i 的第 j 位为 0,则 s[j + n] 加入 s1.
        }
        sum += cnt[make_pair(s1,s2)]; // 因为 s1 和 s2 要相等所以要对称相等
    }
    printf("%lld\n",sum);
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值