AT_abc363_c 题解

· · 题解

思路

纯纯暴力题。

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

代码

# 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
}