码迷,mamicode.com
首页 > 其他好文 > 详细

P4432 [COCI2017-2018#2] ??ZigZag

时间:2019-09-15 12:52:00      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:ace   scan   evo   contain   The   sample   lin   pre   first   

题意翻译

Zig和Zag正在玩文字游戏。Zig说了一个字母,而Zag说了一个以该字母开头的单词。但是这个词需要出现在给出的单词列表中,并且被是相同首字母中使用的次数最少的单词。如果单词的选择不明确(即相同首字母中使用的次数最少的单词不止一个),那么Zag会选择字典序较小的字母。输入保证对于每个Zig的字母,都有可以选择的单词。

假设有一个由K个不同的单词组成的列表和一个Zig给出的N个字母组成的列表。编写一个程序,根据输入,输出Zag在游戏过程中说出的N个单词。

输入输出格式

输入格式: 第一行输入包含来自的正整数K(1≤K≤100 000)和N(1≤N≤100 000)。

以下K行是K个单词,由小写英文字母组成,不超过21个字母。

以下N行是Zig说的N个小写英文字母。

输出格式: N行,分别对应N个Zig的询问。

 

题目描述

Zig and Zag are playing a word game. Zig says one letter, and Zag says a word that starts with that letter. However, the word needs to be from the allowed word list and such that Zag already said it the least amount of times. If the word choice is ambiguous, then Zag will choose the one that is lexicographically smaller (sooner in the alphabet). For each Zig’s letter, it will be possible to choose a word.

Let there be a list consisting of exactly K distinct words and an array of N letters that Zig has given. Write a program that will, based on the input, output an array of N words that Zag said during the game.

输入格式

The first line of input contains positive integers K (1 ≤ K ≤ 100 000) and N (1 ≤ N ≤ 100 000) from the task.

Each of the following K lines contains a single word consisting of lowercase letters of the English alphabet not longer than 21 characters.

Each of the following N lines contains a single lowercase letter of the English alphabet.

输出格式

You must output N lines, each containing a single word from the task.

输入输出样例

输入 #1
4 5
zagreb
split
zadar
sisak
z
s
s
z
z
输出 #1
zadar
sisak
split
zagreb
zadar
输入 #2
5 3
london
rim
pariz
moskva
sarajevo
p
r
p
输出 #2
pariz
rim
pariz
输入 #3
1 3
zagreb
z
z
z
输出 #3
zagreb
zagreb
zagreb

说明/提示

In test cases worth 60% of total points, it will hold that N and K are smaller than 500.

 

 

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
string s[30][10005],S;
char ch;
int n,m,num[30],p[30];
int main(){
	scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
    	cin>>S;
        int bj=S[0]-‘a‘+1;
        s[bj][++num[bj]]=S;
    }
    for(int i=1;i<=26;i++){
		sort(s[i]+1,s[i]+num[i]+1);
	}
    while(m--){
        ch=getchar();
        while(ch<‘a‘||ch>‘z‘){
			ch=getchar();
		}
        int bj=ch-‘a‘+1;
        p[bj]++;
        if(p[bj]>num[bj]){
			p[bj]=1;
		}
        cout<<s[bj][p[bj]]<<endl;
    }
    return 0;
}

  

P4432 [COCI2017-2018#2] ??ZigZag

标签:ace   scan   evo   contain   The   sample   lin   pre   first   

原文地址:https://www.cnblogs.com/xiongchongwen/p/11521781.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!