Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) C. Bear and Different Names 贪心

C. Bear and Different Names

题目连接:

http://codeforces.com/contest/791/problem/C

Description

In the army, it isn’t easy to form a group of soldiers that will be effective on the battlefield. The communication is crucial and thus no two soldiers should share a name (what would happen if they got an order that Bob is a scouter, if there are two Bobs?).

A group of soldiers is effective if and only if their names are different. For example, a group (John, Bob, Limak) would be effective, while groups (Gary, Bob, Gary) and (Alice, Alice) wouldn’t.

You are a spy in the enemy’s camp. You noticed n soldiers standing in a row, numbered 1 through n. The general wants to choose a group of k consecutive soldiers. For every k consecutive soldiers, the general wrote down whether they would be an effective group or not.

You managed to steal the general’s notes, with n - k + 1 strings s1, s2, …, sn - k + 1, each either “YES” or “NO”.

The string s1 describes a group of soldiers 1 through k (“YES” if the group is effective, and “NO” otherwise).
The string s2 describes a group of soldiers 2 through k + 1.
And so on, till the string sn - k + 1 that describes a group of soldiers n - k + 1 through n.
Your task is to find possible names of n soldiers. Names should match the stolen notes. Each name should be a string that consists of between 1 and 10 English letters, inclusive. The first letter should be uppercase, and all other letters should be lowercase. Names don’t have to be existing names — it’s allowed to print “Xyzzzdj” or “T” for example.

Find and print any solution. It can be proved that there always exists at least one solution.

Input

The first line of the input contains two integers n and k (2 ≤ k ≤ n ≤ 50) — the number of soldiers and the size of a group respectively.

The second line contains n - k + 1 strings s1, s2, …, sn - k + 1. The string si is “YES” if the group of soldiers i through i + k - 1 is effective, and “NO” otherwise.

Output

Find any solution satisfying all given conditions. In one line print n space-separated strings, denoting possible names of soldiers in the order. The first letter of each name should be uppercase, while the other letters should be lowercase. Each name should contain English letters only and has length from 1 to 10.

If there are multiple valid solutions, print any of them.

Sample Input

8 3
NO NO YES YES YES NO

Sample Output

Adam Bob Bob Cpqepqwer Limak Adam Bob Adam

Hint

题意

你需要构造n个名字,满足n-k+1个要求。

如果第i个要求是YES的话,那么要求[i,i+k-1]的名字都不相同。

如果第i个要求是NO的话,那么要求[i,i+k-1]的名字至少有两个是相同的。

题解:

如果是YES的话,那么就让[i,i+k-1]里面的点相互连边,连边表示不能取相同的名字。

然后我们就开始贪心构造就好了,在满足不和相连的点取相同名字的前提下,取离自己最近的人的名字就好了。

代码

#include<bits/stdc++.h>
using namespace std;

int n,k;
int flag[55];
int fa[55];
int ans[55];
int tot = 0;
int mp[55][55];
string getid(int x){
    if(x<26){
        string t;
        t+=('A'+x);
        return t;
    }else{
        string t = "A";
        t+=(x-25+'a');
        return t;
    }
}
int vis[55];
int main(){
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n-k+1;i++){
        string op;
        cin>>op;
        if(op[0]=='Y'){
            for(int j=i;j<i+k;j++){
                for(int p=j+1;p<i+k;p++){
                    mp[j][p]=1;
                    mp[p][j]=1;
                }
            }
        }
    }
    for(int i=1;i<=n;i++){
        int Flag = 0;
        memset(vis,0,sizeof(vis));
        for(int j=i-1;j>=1;j--){
            if(mp[i][j]==1){
                vis[ans[j]]=1;
            }
        }
        for(int j=i-1;j>=1;j--){
            if(vis[ans[j]]==0){
                ans[i]=ans[j];
                Flag = 1;
                break;
            }
        }
        if(Flag==0){
            for(int j=0;j<=50;j++){
                if(vis[j]==0){
                    ans[i]=j;
                    break;
                }
            }
        }
    }
    for(int i=1;i<=n;i++){
        cout<<getid(ans[i])<<" ";
    }
    cout<<endl;
}
    原文作者:qscqesze
    原文地址: https://www.cnblogs.com/qscqesze/p/6582753.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞