2017 ICPC WF I Secret Chamber at Mount Rushmore(水)

来源:互联网 发布:js设置div颜色 编辑:程序博客网 时间:2024/04/30 09:23

题目链接:2017 WF I Secret Chamber at Mount Rushmore

Description: 
By now you have probably heard that there is a spectacular stone sculpture featuring four famous U.S.presidents at Mount Rushmore. However, very few people know that this monument contains a secret chamber. This sounds like something out of a plot of a Hollywood movie, but the chamber really exists. It can be found behind the head of Abraham Lincoln and was designed to serve as a Hall of Records to store important historical U.S. documents and artifacts. Historians claim that the construction of the hall was halted in 1939 and the uncompleted chamber was left untouched until the late 1990s, but this is not the whole truth. 
In 1982, the famous archaeologist S. Dakota Jones secretly visited the monument and found that the chamber actually was completed, but it was kept confidential. This seemed suspicious and after some poking around, she found a hidden vault and some documents inside. Unfortunately, these documents did not make any sense and were all gibberish. She suspected that they had been written in a code, but she could not decipher them despite all her efforts. 
Earlier this week when she was in the area to follow the ACM-ICPC World Finals, Dr. Jones finally discovered the key to deciphering the documents, in Connolly Hall of SDSM&T. She found a document that contains a list of translations of letters. Some letters may have more than one translation, and others may have no translation. By repeatedly applying some of these translations to individual letters in the gibberish documents, she might be able to decipher them to yield historical U.S. documents such as the Declaration of Independence and the Constitution. She needs your help. 
You are given the possible translations of letters and a list of pairs of original and deciphered words. Your task is to verify whether the words in each pair match. Two words match if they have the same length and if each letter of the first word can be turned into the corresponding letter of the second word by using the available translations zero or more times.

Input 
The first line of input contains two integers m(1≤m≤500) and n(1≤n≤50), where m is the number of tanslations of letters and n is the number of word pairs. Each of the next m lines contains two distinct space-separated letters a and b, indicating that the letter a can be translated to the letter b.Each ordered pair of letters (a,b) appears at most once. Following this are n lines, each containing a word pair to check. Translations and words use only lowercase letters ‘a’–‘z’, and each word contains at least 1 and at most 50 letters.

Output 
For each pair of words, display yes if the two words match, and no otherwise.

Sample Input 1 
9 5 
c t 
i r 
k p 
o c 
r o 
t e 
t f 
u h 
w p 
we we 
can the 
work people 
it of 
out the

Sample Output 1 
yes 
no 
no 
yes 
yes

Sample Input 2 
3 3 
a c 
b a 
a b 
aaa abc 
abc aaa 
acm bcm

Sample Output 2 
yes 
no 

yes

     

做WF的感觉体验:拿来一道题,数据所见,存之,用之。存何处取决于何种方式能够更好地来组织数据(栈这种东西也只是现实生活某种工具的提炼和抽象,若是没有栈,那能否自发的建栈或者另辟新径,这里是强调的数据结构的重要性),用之,通常在模拟题中的没有很官方的算法,一般技巧性的东西比较强,好的算法是建立在最愚蠢的方法的之上的,因此对于自己的愚笨但是可以Accept的方法应当寻找和好的算法的契合点,优在哪里,好在哪里,一定有着不同。
本题有关字符串的问题,基础题。
题目大意: 
给出n组字母,每组有两个,意味着前者可以转化为后者。在转化过程中遵循传递性。即如果a可以转换为b,b可以转化为c,那么a也可以转换为c。 
给出m组单词,问前者是否可以通过若干次转换转换为b。
大体思路:
本题重在“转化”、“判断”,转化可以等价于映射,转化的传递性等价于复合函数,可以用二维数组储存可相互的转化的两个字符,Value为true或者false代表是否可以进行转化,这也为判断提供了便利

#include <bits/stdc++.h>using namespace std;typedef long long LL;const int MAX = 1005;const int MOD = 1e9+7;const int INF = 0x3f3f3f3f;bool a[26][26];int main(){    int n, m;    cin >> n >> m;    for(int i = 0; i < 26; ++i) a[i][i] = true;    char aa, bb;    for(int i = 0; i < n; ++i)    {        cin >> aa >> bb;        a[aa-'a'][bb-'a'] = true;    }    for(int k = 0; k < 26; ++k)        for(int i = 0; i < 26; ++i)            for(int j = 0; j < 26; ++j)                a[i][j] |= a[i][k] & a[k][j];    string s, l;    for(int i = 0; i < m; ++i)    {        cin >> s >> l;        bool flag = s.length()==l.length();        if(flag)        {            for(int j = 0; j < s.length(); ++j)                if(!a[s[j]-'a'][l[j]-'a'])                {                    flag = false;                    break;                }        }        if(flag)            puts("yes");        else            puts("no");    }    return 0;}