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, numbered1 through n. The general wants to choose a group ofk 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 stringss1, s2, ..., sn - k + 1, each either "YES" or "NO".
- The string s1 describes a group of soldiers1 through k ("YES" if the group is effective, and "NO" otherwise).
- The string s2 describes a group of soldiers2 through k + 1.
- And so on, till the string sn - k + 1 that describes a group of soldiersn - 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 between1 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.
The first line of the input contains two integers n andk (2 ≤ k ≤ n ≤ 50) — the number of soldiers and the size of a group respectively.
The second line contains n - k + 1 stringss1, s2, ..., sn - k + 1. The stringsi is "YES" if the group of soldiersi through i + k - 1 is effective, and "NO" otherwise.
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 from1 to 10.
If there are multiple valid solutions, print any of them.
8 3 NO NO YES YES YES NO
Adam Bob Bob Cpqepqwer Limak Adam Bob Adam
9 8 YES NO
R Q Ccccccccc Ccocc Ccc So Strong Samples Ccc
3 2 NO NO
Na Na Na
In the first sample, there are 8 soldiers. For every3 consecutive ones we know whether they would be an effective group. Let's analyze the provided sample output:
- First three soldiers (i.e. Adam, Bob, Bob) wouldn't be an effective group because there are two Bobs. Indeed, the strings1 is "NO".
- Soldiers 2 through 4 (Bob, Bob, Cpqepqwer) wouldn't be effective either, and the strings2 is "NO".
- Soldiers 3 through 5 (Bob, Cpqepqwer, Limak) would be effective, and the strings3 is "YES".
- ...,
- Soldiers 6 through 8 (Adam, Bob, Adam) wouldn't be effective, and the strings6 is "NO".
给你N个数,每连续的K个数算作一个区域,一个区域中如果没有重复的名字,那么是YES,否则就是NO.
给你这些区域的YES和NO的情况,让你构造一个可行解。
思路:
1、首先我们处理YES的区域,将所有YES的区域都可以很简单的设定为相互不同的名字。
2、接下来我们再处理NO的区域,处理NO的区域之前,我们可以将所有没有放置名字的位子设定为相互不同的名字。
如果遇到了一个NO的区域,那么考虑相邻两个区域的特性:
假设有:
1234
2345
第二个区域相对第一个区域少了一个1,多了一个5.那么我们如果第一个区域是NO,我们不妨将14设定为相同名字,这时候再到下一个区域的时候,234不会相互影响。
那么我们处理NO的区域的时候,将区域的开头和结尾设定为相同的名字即可。
3、过程模拟注意细节。
Ac代码:
#include<stdio.h>
#include<string.h>
using namespace std;
int ans[55];
char a[55][55];
int main()
{
char name[1005][55];
char fst='A';
char sec='a';
for(int i=1;i<=1000;i++)
{
for(int j=0;j<2;j++)
{
if(j==0)name[i][j]=fst;
else name[i][j]=sec,sec++;
if(sec>'z')fst++,sec='a';
}
name[i][2]='\0';
}
int n,k;
while(~scanf("%d%d",&n,&k))
{
int now=1;
memset(ans,-1,sizeof(ans));
for(int i=0;i<n-k+1;i++)
{
scanf("%s",a[i]);
if(strcmp(a[i],"YES")==0)
{
for(int j=i;j<i+k;j++)
{
ans[j]=now;
now++;
}
}
}
for(int i=0;i<n;i++)
{
if(ans[i]==-1)ans[i]=now,now++;
}
for(int i=0;i<n-k+1;i++)
{
if(strcmp(a[i],"NO")==0)
{
ans[i+k-1]=ans[i];
}
}
for(int i=0;i<n;i++)
{
printf("%s ",name[ans[i]]);
}
printf("\n");
}
}