http://codeforces.com/problemset/problem/499/B
You have a new professor of graph theory and he speaks very quickly. You come up with the following plan to keep up with his lecture and make notes.
You know two languages, and the professor is giving the lecture in the first one. The words in both languages consist of lowercase English characters, each language consists of several words. For each language, all words are distinct, i.e. they are spelled differently. Moreover, the words of these languages have a one-to-one correspondence, that is, for each word in each language, there exists exactly one word in the other language having has the same meaning.
You can write down every word the professor says in either the first language or the second language. Of course, during the lecture you write down each word in the language in which the word is shorter. In case of equal lengths of the corresponding words you prefer the word of the first language.
You are given the text of the lecture the professor is going to read. Find out how the lecture will be recorded in your notes.
The first line contains two integers, n and m (1 ≤ n ≤ 3000, 1 ≤ m ≤ 3000) — the number of words in the professor's lecture and the number of words in each of these languages.
The following m lines contain the words. The i-th line contains two strings ai, bi meaning that the word ai belongs to the first language, the word bi belongs to the second language, and these two words have the same meaning. It is guaranteed that no word occurs in both languages, and each word occurs in its language exactly once.
The next line contains n space-separated strings c1, c2, ..., cn — the text of the lecture. It is guaranteed that each of the strings cibelongs to the set of strings {a1, a2, ... am}.
All the strings in the input are non-empty, each consisting of no more than 10 lowercase English letters.
Output exactly n words: how you will record the lecture in your notebook. Output the words of the lecture in the same order as in the input.
4 3
codeforces codesecrof
contest round
letter message
codeforces contest letter contest
codeforces round letter round
5 3
joll wuqrd
euzf un
hbnyiyc rsoqqveh
hbnyiyc joll joll euzf joll
hbnyiyc joll joll un joll
题解:题目很简单,就是给两组字符串映射,再给你一串字符串,对于串的每个单词,都在映射对儿里选择长度最短的那个。之所以这里还要写解题报告,是因为我wa了一发,wa在map的find的概念上。对于字符串,map里存的是一个地址值。我代码里字符串是new出来的,同一个字符串,前后两次地址值不同,直接find是找不到的,需要遍历,且用strcmp比较。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map> using namespace std; const int N=;
map<char*,char*> ma;
map<char*,char*>::iterator it;
int n,m; int main(){
char* s,*t;
scanf("%d%d",&n,&m);
for(int i=;i<m;i++){
s=new char[N];
scanf("%s",s);
t=new char[N];
scanf("%s",t);
ma[s]=t;
}
for(int i=;i<=n;i++){
s=new char[N];
scanf("%s",s);
int l1=strlen(s);
for(it=ma.begin();it!=ma.end();it++){//需要注意一点就是,这里不能直接用it=ma.find(s)。因为ma里存的都是字符串的地址值,而s是new出来的,即便字符串内容一样,但是地址值是不同的,find的结果一定是end()。
if(!strcmp(s,it->first))//这里只能用strcmp,而不能用==。
break;
}
t=it->second;
int l2=strlen(t);
if(l1>l2) printf("%s",t);
else printf("%s",s);
if(i==n) puts("");
else putchar(' ');
}
return ;
}