Alien code

Today’s competition did a very simple basic grammatical water question. Because I didn’t use the structure + string type for a long time, I made a mistake twice. I remind myself that I should review the past and learn the new.

Title description

Xiao Ming discovered an alien civilization. As a translator, he obtained a word comparison table between alien languages ​​and alien languages. Now there is an alien sentence that needs to be translated into an alien language and sent to the aliens.

enter

The two integers n and m in the first line represent the number of word pairs in the word comparison table and the number of words in the sentence to be translated. The
following n lines, each line contains two strings, the first one represents the word on the alien planet, and the second represents the corresponding Words in Alien Languages
There are m words in the last line, which represent sentences in alien languages ​​to be translated. There is a space between each word
1<=n,m<=10000

Output

One line represents a sentence translated into an alien language, with a space between each word. If a word cannot be found from the comparison table, output "*" with the same length as the
word . Each word length is less than 15

Sample input

5 4
abc asd
hjk dfgr
sd dengd
koi miu
ssd hhd hjk
aaa ssd abc

Sample output

dfgr *** hhd asd

Problem-solving ideas (personal)

Note: (both are some simple grammatical problems)
Use a structure to store the string, and use the string type in C++ to declare the string. Here you need to pay attention to the input and calling methods of the structure members, as well as some commonly used functions in the string type. use.

AC code

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
const int N=10010;
struct qq
{
    
    
    string a[N];
    string b[N];
    string s[N];
}qq;

int main()
{
    
    
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        cin>>qq.a[i]>>qq.b[i];  //注意结构体成员的输入方式
    for(int i=1;i<=m;i++)
        cin>>qq.s[i];
    for(int i=1;i<=m;i++)
    {
    
    
        int t=0;
        int f=qq.s[i].size();   //size()函数可计算字符串中字母个数
        for(int j=1;j<=n;j++)
        {
    
    
            if(qq.s[i]==qq.a[j])  //string类型字符串可直接进行比较
            {
    
    
                t=1;
                cout<<qq.b[j];
                break;
            }
        }
        if(t==0)
        {
    
    
            for(int i=1;i<=f;i++)
            {
    
    
                cout<<"*";
            }
        }
        cout<<" ";
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_46009744/article/details/114037921