AtCoder备赛刷题 ABC 363 | Avoid Palindrome 2

学习C++从娃娃抓起!记录下AtCoder(日本算法竞技网站)备赛学习过程中的题目,记录每一个瞬间。

附上汇总贴:AtCoder 备赛刷题 | 汇总


【Problem Statement】

You are given a string S S S of length N N N consisting only of lowercase English letters.

给定一个长度为 N N N 的字符串 S S S,仅由小写英文字母组成。

Find the number of strings obtained by permuting the characters of S S S (including the string S S S itself) that do not contain a palindrome of length K K K as a substring.

查找通过排列 S S S 的字符(包括字符串 S S S 本身)而获得的字符串数量,其中不包含长度为 K K K 的回文作为子字符串。

Here, a string T T T of length N N N is said to “contain a palindrome of length K K K as a substring” if and only if there exists a non-negative integer ii not greater than ( N − K ) (N−K) (NK) such that T i + j = T i + K + 1 − j T_{i+j}=T_{i+K+1−j} Ti+j=Ti+K+1j for every integer j j j with 1 ≤ j ≤ K 1≤j≤K 1jK.

这里,长度为 N N N 的字符串 T T T 被称为“包含长度为 K K K 的回文作为子字符串”,当且仅当存在一个不大于 ( N − K ) (N−K) (NK) 的非负整数 i i i,使得对于每个整数 j j j T i + j = T i + K + 1 − j T_{i+j}=T_{i+K+1−j} Ti+j=Ti+K+1j ,且 1 ≤ j ≤ K 1≤j≤K 1jK

Here, T k T_k Tk denotes the k k k-th character of the string T T T.

这里, T k T_k Tk 表示字符串 T T T 的第 k k k 个字符。

【Constraints】

  • 2 ≤ K ≤ N ≤ 10 2≤K≤N≤10 2KN10
  • N N N and K K K are integers.
  • S S S is a string of length N N N consisting only of lowercase English letters.

【Input】

The input is given from Standard Input in the following format:

N K 
S

【Output】

Print the number of strings obtained by permuting S S S that do not contain a palindrome of length K K K as a substring.

【Sample Input 1】

3 2
aab

【Sample Output 1】

1

The strings obtained by permuting aab are aab, aba, and baa. Among these, aab and baa contain the palindrome aa of length 2 2 2 as a substring.
Thus, the only string that satisfies the condition is aba, so print 1 1 1.

【Sample Input 2】

5 3
zzyyx

【Sample Output 2】

16

There are 30 30 30 strings obtained by permuting zzyyx, 16 16 16 of which do not contain a palindrome of length 3 3 3. Thus, print 16 16 16.

【Sample Input 3】

10 5
abcwxyzyxw

【Sample Output 3】

440640

【代码详解】

《AtCoder Avoid K Palindrome 2》 #枚举#

#include 
using namespace std;
const int N = 15;
int n, k, ans;
bool st[N];
char a[N], path[N];
map<string, int> mp;
int check()
{
    for (int i=0; i+k-1<n; i++)
    {
        bool flag = true;
        int j = i+k-1;
        for (int x=i, y=j; x<y; x++, y--)
            if (path[x]!=path[y]) flag = false;
        if (flag) return 0;
    }
    return 1;
}
void dfs(int u)
{
    if (u==n)
    {
        string s="";
        for (int i=0; i<n; i++)
            s += path[i];
        if (!mp[s]) 
        {
            mp[s] = 1;
            ans += check();
        }
        return;
    }
    for (int i=0; i<n; i++)
    {
        if (!st[i])
        {
            path[u] = a[i];
            st[i] = true;
            dfs(u+1);
            st[i] = false;
        }
    }
}
int main()
{
    cin >> n >> k;
    for (int i=0; i<n; i++)
        cin >> a[i];
    sort(a, a+n);
    dfs(0);
    cout << ans << endl;
    return 0;
}

【运行结果】

3 2
aab
1

你可能感兴趣的:(c++,算法)