跳到正文

题解

AT_abc363_c 题解

Vitamin_B约 2 分钟
原文
文章信息与版本

文章信息

发布
2024年7月22日
收录
2025年12月12日
更新
2026年11月9日
篇幅
约 2 分钟
获赞
1

版本

  1. v1当前

思路

纯纯暴力题。

第一步,先 dfs 枚举可以构成的字符串并去重(当然也可以放进 set 自动去重)。第二步,对每个字符串暴力截取所有长度为 kk 的子串。第三步,暴力判断是否回文。完事!

代码

C++
# include <bits/stdc++.h>
# define I return
# define AK 0
# define IOI ;
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
int n, k, sum;
string a;
set <string> st;
void dfs (int x) {
	if (x >= n) {
		st.insert (a);
		return ;
	}
	for (int i = x; i < n; ++ i)
		swap (a[i], a[x]), dfs (x + 1), swap (a[i], a[x]);
	return ;
}
bool hw (const string& a) {
	string b = a;
	reverse (b.begin (), b.end ());
	return a == b;
}
bool check (const string& a) {
	for (int i = 0; i <= n - k; ++ i)
		if (hw (a.substr (i, k)))
			return 0;
	return 1;
}
int main () {
	ios::sync_with_stdio (0);
	cin.tie (0);
	cout.tie (0);
	cin >> n >> k >> a;
	dfs (0);
	for (const string& i : st)
		sum += check (i);
	cout << sum;
	I AK IOI
}
原文