练习cf499B. 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 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.

题目大意
现有两个语言(拼写不同)表示同一单词,对现有的n个单词选择更短的作为替代

题目分析
既然有两种单词代表同一含义,可以用map<string,string>的形式,将第一种语言的表示,映射到第二种,再进行查表一一对应找出更短的单词,

点击查看代码
map<string, string> lan;
    for(int i = 0; i < m; i++){
        string l1, l2;
        cin >> l1 >> l2;
        lan[l1] = l2;
    }

完整代码

点击查看代码
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;

int main(){
    int n, m;
    scanf("%d%d", &n, &m);
    map<string, string> lan;
    for(int i = 0; i < m; i++){
        string l1, l2;
        cin >> l1 >> l2;
        lan[l1] = l2;
    }
    for(int i = 0; i < n; i++){
        string word;
        cin >> word;
        string alt = lan[word];
        if (alt.length() < word.length())
            cout << alt << " ";
        else
            cout << word << " ";
    }
    return 0;
}

posted @ 2025-07-09 21:09  sirro1uta  阅读(12)  评论(0)    收藏  举报