cf499B-Lecture 【map】

來源:互聯網
上載者:User

標籤:

http://codeforces.com/problemset/problem/499/B

B. Lecture  

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.

Input

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

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.

Sample test(s)input
4 3
codeforces codesecrof
contest round
letter message
codeforces contest letter contest
output
codeforces round letter round
input
5 3
joll wuqrd
euzf un
hbnyiyc rsoqqveh
hbnyiyc joll joll euzf joll
output
hbnyiyc joll joll un joll

 

 

題解:題目很簡單,就是給兩組字串映射,再給你一串字串,對於串的每個單詞,都在映射對兒裡選擇長度最短的那個。之所以這裡還要寫解題報告,是因為我wa了一發,wa在map的find的概念上。對於字串,map裡存的是一個地址值。我代碼裡字串是new出來的,同一個字串,前後兩次地址值不同,直接find是找不到的,需要遍曆,且用strcmp比較。

代碼:

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <map> 5  6 using namespace std; 7  8 const int N=15; 9 map<char*,char*> ma;10 map<char*,char*>::iterator it;11 int n,m;12 13 int main(){14     char* s,*t;15     scanf("%d%d",&n,&m);16     for(int i=0;i<m;i++){17         s=new char[N];18         scanf("%s",s);19         t=new char[N];20         scanf("%s",t);21         ma[s]=t;22     }23     for(int i=1;i<=n;i++){24         s=new char[N];25         scanf("%s",s);26         int l1=strlen(s);27         for(it=ma.begin();it!=ma.end();it++){//需要注意一點就是,這裡不能直接用it=ma.find(s)。因為ma裡存的都是字串的地址值,而s是new出來的,即便字串內容一樣,但是地址值是不同的,find的結果一定是end()。28             if(!strcmp(s,it->first))//這裡只能用strcmp,而不能用==。29                 break;30         }31         t=it->second;32         int l2=strlen(t);33         if(l1>l2)   printf("%s",t);34         else    printf("%s",s);35         if(i==n)    puts("");36         else    putchar(‘ ‘);37     }38     return 0;39 }

cf499B-Lecture 【map】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.