题意翻译
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.
输入输出样例
4 5zagrebsplitzadarsisakzsszz
zadarsisaksplitzagrebzadar
5 3londonrimparizmoskvasarajevoprp
parizrimpariz
1 3zagrebzzz
zagrebzagrebzagreb
说明/提示
In test cases worth 60% of total points, it will hold that N and K are smaller than 500.
#include#include #include #include #include #include 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< <