• UVALive 6523 Languages


    传送门

    The Enterprise has encountered a planet that at one point had been inhabited. The only remnant from the prior civilization is a set of texts that was found. Using a small set of keywords found in various different languages, the Enterprise team is trying to determine what type of beings inhabited the planet.

    Input

    The first line of input will be N (1 ≤ N ≤ 100), the number of different known languages. The next N lines contain, in order, the name of the language, followed by one or more words in that language, separated with spaces. Following that will be a blank line. After that will be a series of lines, each in one language, for which you are to determine the appropriate language. Words consist of uninterrupted strings of upper or lowercase ASCII letters, apostrophes, or hyphens, as do the names of languages. No words will appear in more than one language. No line will be longer than 256 characters. There will be at most 1000 lines of sample text. Every sample text will contain at least one keyword from one of the languages. No sample text will contain keywords from multiple languages. The sample text may contain additional punctuation (commas, periods, exclamation points, semicolons, question marks, and parentheses) and spaces, all of which serve as delimiters separating keywords. Sample text may contain words that are not keywords for any specific language. Keywords should be matched in a case-insensitive manner.

    Output

    For each line of sample text that follows the blank line separating the defined languages, print a single line that identifies the language with which the sample text is associated.

    Sample Input

    4

    Vulcan throks kilko-srashiv k’etwel

    Romulan Tehca uckwazta Uhn Neemasta

    Menk e’satta prah ra’sata

    Russian sluchilos

    Dif-tor heh, Spohkh. I’tah trai k’etwel

    Uhn kan’aganna! Tehca zuhn ruga’noktan!

    Sample Output

    Vulcan

    Romulan

    Solution

    注意Input加粗的部分,我在这里WA了一发。

    这题的输入是个坑。首先考虑如何读取Keywords,这可归结为读入一行内的若干字符串

    在本题中,一行输入可分为两部分:words(词), delimiters(定界符、分隔符)。

    delimiters又分为两种:whitespace characters(空白字符), punctuations(标点)。

    keywords部分的输入,delimiters不含puntuations。如果没有读入一行这个设定的话,

    char s[300];
    
    scanf("%s", s);

    就搞定了。加了这个限制,只要把每个word后面的delimiters也读入,看其中是否包含' '就好了:

    char s[300], delim[300];
    
    scanf("%s%[ 
    ]", s, delim);

    这里有个地方要特别注意:

    在Windows下换行符是' ' (line feed + carriage return),此时按上面代码中的方式读取delimiters就不能读取/r,但Windows下的换行符并不总是' ',也有只是' '的情况,可能跟文件的扩展名有关吧,这个问题我暂不清楚。我调试时是从无扩展名的文本文件in读入的,换行符是' ',不知换成in.txt会如何。但无论如何

    char s[300], delim[300];
    
    scanf("%s%[ 
    
    ]", s, delim);

    都能正确处理。

    -------------------------------------------------------------------------------------------------------------

     sample text的输入要多考虑punctuations,可如此处理:

    char s[300], delim[300];
    
    scanf("%*[,.!;() 
    ]"), scanf("%[^,.!;() 
    ]%[,.!;() 
    ]", s, delim);

    这样写其实有点多余,下面Implementation里的第二份代码给出了另一种做法。

    注意这里如果写成

    char s[300], delim[300];
    
    scanf("%*[,.!;() 
    ]%[^,.!;() 
    ]%[,.!;() 
    ]", s, delim);

    就错了。(请考虑函数scanf()在什么情况下return)

    Implementation

    #include <bits/stdc++.h>
    using namespace std;
    const int N(105);
    map<string, int> mp;
    char lang[N][300], s[300], delim[300];
    string t;
    
    int main(){
        int n;
        scanf("%d", &n);
        for(int i=0; i<n; i++){
            scanf("%s", lang[i]);
            for(bool flag=false; !flag;){
                scanf("%s%[ 
    ]", s, delim);
                for(int i=0; s[i]; i++)
                    s[i]=tolower(s[i]);
                t=s;
                mp[t]=i;
                for(int i=0; delim[i]; i++)
                    if(delim[i]=='
    '){
                        flag=true;
                        break;
                    }
            }
        }
    
        for(bool flag=false; scanf("%*[,.!;() 
    ]"), ~scanf("%[^,.!;() 
    ]%[,.!;() 
    ]", s, delim); ){
            if(!flag){
                for(int i=0; s[i]; i++)
                    s[i]=tolower(s[i]);
                t=s;
                if(mp.find(t)!=mp.end()){
                    flag=true;
                    puts(lang[mp[t]]);
                }
            }
            for(int i=0; delim[i]; i++)
                if(delim[i]=='
    '){
                    flag=false;
                    break;
                }
        }
    }
    #include <bits/stdc++.h>
    using namespace std;
    const int N(105);
    map<string, int> mp;
    char lang[N][300], s[300], delim[300];
    string t;
    
    int main(){
        int n;
        scanf("%d", &n);
        for(int i=0; i<n; i++){
            scanf("%s", lang[i]);
            for(bool flag=false; !flag;){
                scanf("%s%[ 
    ]", s, delim);
                for(int i=0; s[i]; i++)
                    s[i]=tolower(s[i]);
                t=s;
                mp[t]=i;
                for(int i=0; delim[i]; i++)
                    if(delim[i]=='
    '){
                        flag=true;
                        break;
                    }
            }
        }
        scanf("%*[,.!;() 
    ]");
        for(bool flag=false; ~scanf("%[^,.!;() 
    ]%[,.!;() 
    ]", s, delim); ){
            if(!flag){
                for(int i=0; s[i]; i++)
                    s[i]=tolower(s[i]);
                t=s;
                if(mp.find(t)!=mp.end()){
                    flag=true;
                    puts(lang[mp[t]]);
                }
            }
            for(int i=0; delim[i]; i++)
                if(delim[i]=='
    '){
                    flag=false;
                    break;
                }
        }
    }
  • 相关阅读:
    百度地图API地理位置和坐标转换
    解决Myeclipse中导入自定义的配色方案后,JSP中的js代码块为白色背景的问题
    Windows 平台下Myeclipse 10 中SVN 插件使用教程(TortoiseSVN)
    Java代码通过API操作HBase的最佳实践
    HBase中多Filter查询示例
    使用Ant编译和构建项目指南
    如何在Google Play商店发布多个版本apk
    Android 设备管理API概览(Device Administration API)
    Android呼叫管理服务之会话发起协议(SIP)API
    Android基础知识之String类使用详解
  • 原文地址:https://www.cnblogs.com/Patt/p/5002052.html
Copyright © 2020-2023  润新知