ABC 249 C - Just K(dfs)

https://atcoder.jp/contests/abc249/tasks/abc249_c

题目大意:

给定n个字符串,让我们随意选择,然后找到这里面相同的字母刚好等于k个的时候的数量是多少?

求可选择出来的最大数量的不同字母数量是多少?
Sample Input 1  
4 2
abi
aef
bc
acg
Sample Output 1  
3

Sample Input 2  
2 2
a
b
Sample Output 2  
0

Sample Input 3 
5 2
abpqxyz
az
pq
bc
cy
Sample Output 3  
7
  • 每个字符串都面临着选还是不选的境地,而且字符数量较少;所以我们可以直接dfs爆搜
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=200200,M=2002;
LL n,k,maxn=0;
string s[N];
void dfs(LL idx,string c)
{
    LL sum=0;
    map<char,LL> mp;
    for(LL i=0;i<c.size();i++)
    {
        mp[c[i]]++;
        if(mp[c[i]]==k) sum++;
        else if(mp[c[i]]==k+1) sum--;
    }
    mp.clear();
    maxn=max(maxn,sum);
    if(idx==n+1)
    {
        return ;
    }
    dfs(idx+1,c+s[idx]);
    dfs(idx+1,c);
}
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        cin>>n>>k;
        for(LL i=1;i<=n;i++)
        {
            cin>>s[i];
        }
        dfs(1,"");
        cout<<maxn<<endl;
    }
    return 0;
}
posted @ 2022-10-05 16:07  高尔赛凡尔娟  阅读(30)  评论(0编辑  收藏  举报