Codeforces Round #284 (Div. 2)---B. Lecture (暴力)

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 ≤ 30001 ≤ 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 aibi 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 ci belongs 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




题意:有m对单词是可以互相替代的,给出的顺序是一行给出两个单词,中间用空格分开,第一个被称作第一语言,第二个叫第二语言。最后给出n个单词组成的文本,让你找出能够替换成的单词长度最短的文本,注意,当第一语言和第二语言长度相等的时候,取第一语言。输出替换后的文本即可。


分析:这个其实也好弄。直接用map映射一下,开两个map<string, string> a,map<string, int> b,每读入一对同义词s和t,就将两者的互相映射都加入到a中,再纪录优先级b[s] = 1, b[t] = 0,全部读入后,再扫描文本,直接判断应该选择那个单词,优先选择长度最小的同义词,长度相同的情况下,再按优先级取,直接输出即可。




AC代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define INF 0x7fffffff

map<string, string> a;
map<string, int> b;

int main()
{
    #ifdef sxk
        freopen("in.txt","r",stdin);
    #endif
    int n, m;
    while(scanf("%d%d",&n, &m)!=EOF)
    {
        string s, t;
        a.clear();  b.clear();
        for(int i=0; i<m; i++){
            cin>>s>>t;
            a[s] = t;           //建立映射
            a[t] = s;
            b[s] = 1;           //纪录优先级(第一语言优先)
            b[t] = 0;
        }
        for(int i=0; i<n; i++){
            cin>>s;
            int x = a[s].size(), xx = s.size();
            if(x < xx) cout<<a[s];         //选择长度小的同义词
            else if(x > xx) cout<<s;       
            else{                        
                if(b[s]) cout<<s;          //等长时,取第一语言
                else cout<<a[s];
            }
            if(i < n-1) cout<<" ";
        }
        puts("");
    }
    return 0;
}



PS:map还是挺好用的,昨天DoubleQ讲用hash搞,一听就不懂,至今还没用hash搞过题呢。。。不过很明显直接用map就暴力切掉了^_^



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值