P4432 [COCI2017-2018#2] ​​ZigZag

题意翻译

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;
}

  

转载于:https://www.cnblogs.com/xiongchongwen/p/11521781.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值