首页 > 试题广场 >

游游的字符重排

[编程题]游游的字符重排
  • 热度指数:1946 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
游游定义一个字符串是“好串”,当且仅当该字符串相邻的字符不相等。例如"arcaea"是好串,而"food"不是好串。

游游拿到了一个字符串,她可以将该字符串的各个字符顺序随意打乱。她想知道一共可以生产多少种不同的好串?

输入描述:
一个仅包含小写字母的字符串,长度不超过10。


输出描述:
好串的数量。
示例1

输入

aab

输出

1

说明

只有"aba"这一种好串。
示例2

输入

arc

输出

6
示例3

输入

aaa

输出

0
头像 LuoxuanLove
发表于 2025-12-03 02:07:56
虽然这题暴力即可,但这题一看就有组合DP做法,而且系数固定,显然可以用多项式科技优化,遂让AI写了一下此做法。这是一个基于容斥原理结合生成函数的组合计数问题,使用NTT进行优化。算法思路容斥原理转化:我们要计算没有相邻字符相等的排列数。直接计算比较困难,我们使用容斥原理。对于每种字符  ,假设它的出 展开全文
头像 Lambda_L
发表于 2025-12-03 00:18:14
思路字符串长度很短,可以用dfs,先将每个字符的数量存起来,然后枚举新串每个字符。记得回溯。ACnode #include<bits/stdc++.h> using namespace std; map<char,int>cnt; int len;//字符串长度 int an 展开全文
头像 25软工王瑞挺_叫我王神
发表于 2025-12-03 09:02:12
//最后一舞 #include<bits/stdc++.h> using namespace std; #define out(x) cout << #x << '=' << (x) << endl #define out2(x, y) c 展开全文
头像 ddb酱
发表于 2025-12-03 00:15:04
#include <bits/stdc++.h> using namespace std; #define endl "\n" #define all(a) a.begin(), a.end() void solve() { string s;cin > 展开全文
头像 _已被标记为牛弊_Refrain_Y
发表于 2025-12-03 00:45:38
因为很小,直接dfs枚举就可以,主要是看如何去重这里是一种去重思路,将原来字符串排序然后直接看上一个是否使用过,如果没有使用过那说明这两个(多个)都不能用,所以不考虑放入 #include <iostream> #include <vector> #include <c 展开全文
头像 周康禧
发表于 2025-12-03 15:11:42
#include <bits/stdc++.h> using namespace std; using ll = long long int; using ld = long double; using PII=pair<ll,ll>; using PIII=pair< 展开全文
头像 以诚丶
发表于 2025-07-15 16:40:48
数据范围很小,极端情况下全是不同的字母,那么有种情况,直接dfs搜索所有可能即可。 import sys from collections import Counter # 输入加速 input = sys.stdin.readline if __name__ == '__main__': 展开全文
头像 mollor
发表于 2025-12-03 00:17:28
注意到,长度不大于十所以能够直接全排列草过去 #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll 展开全文
头像 此在Dasein
发表于 2025-12-03 06:18:18
算法思路 本题要求统计给定字符串(长度 ≤ 10)的所有不同排列中,满足“相邻字符不相等”(即好串)的排列个数。由于长度很小,可以直接枚举所有可能的排列,但需注意重复字符会导致重复排列,因此采用回溯法 + 剪枝: 先统计每个字符出现的频率,然后通过深度优先搜索(DFS)构造字符串。 构造过程中,每 展开全文
头像 logcjj
发表于 2025-12-03 12:22:31
#include<bits/stdc++.h> using namespace std; int ans; int n; string a; int st[100]; int dfs(int end,string now) { if(end==n) { ans++; 展开全文