zoukankan      html  css  js  c++  java
  • ZOJ Monthly, November 2012 I Search in the Wiki

    Search in the Wiki

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    As we known, Searching in Wiki is an useful way for everyone who wants to get information. Wiki, a website which allows its users to add, modify, or delete its content via a web browser, is famous for its huge information. You can find almost anything you heard in Wiki.

    But sometimes you may get into trouble because of these huge information. It's hard to find key words from so many characters, that means you will spend a lot of time understanding what it describes. To solve this question, wiki provides some tips for each word. The tips for one word describe the meaning of this word briefly, so you can understand this word quickly through these tips. A tip consists only of 'a' to 'z' and 'A' to 'Z'. It's a convenient application.

    This time you get a task from your teacher to search information for given words. It's a boring work, so you think of Wiki immediately. You get tips for each word from Wiki, and now you can answer questions the teacher may ask tomorrow morning easily. But to make sure, you decide to test yourself before tomorrow.

    You prepare some queries for the test, each query contains some words given before and you should find out all the common tips of words in this query (A common tip means all the words in the query have this tip). In order to check your answer, you need to write a program now.

    Input

    There are multiple test cases.

    Each case begins with an integer n ( 1 <=n <=100 ), indicating the number of words given. Next N*2 lines, each two lines describe a word and its tips. For each two lines, the first line gives an word ( the word is no longer than 30 characters) , and the second line contains some tips for this word (the string is no longer than 200 characters), each two words are separated by one space.

    The N*2+2 line contains an integer m ( 1 <=m <= 100 ), indicating the number of queries. Next m lines, each line contains some words, each two words are separated by one space.( the string is no longer than 200 characters)

    Process to the end of input.

    Output

    For each query, print one line with all the common tips of the words in query, and each two words are separated by one space. (The common tips should be printed in alphabet order) If no tips satisfy the previous request, print one line with "NO".

    Sample Input

    4
    fish
    agile animal
    horse
    swift animal
    eagle
    fierce animal
    Kyuubee
    alien incubator
    2
    fish horse eagle
    fish horse eagle Kyuubee
    

    Sample Output

    animal
    NO
    

    分析:直接用vector和map暴力,主要用到的是set_intersection()函数

    #include<cstring>
    #include<cstdio>
    #include<set>
    #include<string>
    #include<vector>
    #include<iostream>
    #include<map>
    #include<iterator>
    #include<algorithm>
    using namespace std;
    
    char name[35], s[210], tips[210];
    int check[110];
    
    int main() {
        int n, m, i, j, a, b, cntw, cntt, cnt, cntc;
        while (scanf("%d", &n) != EOF) {
            cntw = cntt = 0;
            map<string, int> word;
            map<string, int> tip;
            map<int, string> totip;
            vector <int> adj[110];
            vector <int> inter, Inter;
            for (j = 1; j <= n; ++j) {
                scanf("%s", name);
                if (!word[name]) {
                    word[name] = ++cntw;
                }
                a = word[name];
                scanf("%*c");
                gets(s);
                //   printf("%s:",name);
                //  puts(s);
                cnt = 0;
                for (i = 0; s[i]; ++i) {
                    if (s[i] == 32) {
                        tips[cnt] = 0;
                        if (!tip[tips]) {
                            tip[tips] = ++cntt;
                            totip[cntt] = tips;
                        }
                        b = tip[tips];
                        adj[a].push_back(b);
                        cnt = 0;
                    } else
                        tips[cnt++] = s[i];
                }
                tips[cnt] = 0;
                if (!tip[tips]) {
                    tip[tips] = ++cntt;
                    totip[cntt] = tips;
                }
                b = tip[tips];
                adj[a].push_back(b);
                sort(adj[a].begin(), adj[a].end());
            }
            scanf("%d", &m);
            scanf("%*c");
            while (m--) {
                //      printf("m=%d\n", m);
                cntc = 0;
                gets(s);
                //     puts(s);
                cnt = 0;
                for (i = 0; s[i]; ++i) {
                    if (s[i] == 32) {
                        name[cnt] = 0;
                        //  printf("%s\n",name);
                        check[cntc++] = word[name];
                        cnt = 0;
                    } else
                        name[cnt++] = s[i];
                }
                name[cnt] = 0;
                check[cntc++] = word[name];
                //      for (i = 0; i < cntc; ++i)
                //         printf("%d:%d\n", i, check[i]);
                inter.clear();
                for (vector<int> ::iterator p = adj[check[0]].begin(); p < adj[check[0]].end(); ++p) {
                    inter.push_back(*p);
                }
    
                for (i = 1; i < cntc; ++i) {
                    //      printf("i=%d\n", i);
                    Inter.clear();
                    //        for (vector<int> ::iterator p = Inter.begin(); p < Inter.end(); ++p)
                    //            printf("%d kkkkkk\n", *p); 
                    //        for (vector<int> ::iterator p = adj[check[i]].begin(); p < adj[check[i]].end(); ++p)
                    //             printf("%d **adj4*\n", *p);
                    set_intersection(inter.begin(), inter.end(), adj[check[i]].begin(), adj[check[i]].end(), back_inserter(Inter));
                    ++i;
    
                    if (i >= cntc) {
                        inter.clear();
                        for (vector<int> ::iterator p = Inter.begin(); p < Inter.end(); ++p) {
                            inter.push_back(*p);
                            //     printf("%d @@@\n", *p);
                        }
                        break;
                    }
                    inter.clear();
                    set_intersection(Inter.begin(), Inter.end(), adj[check[i]].begin(), adj[check[i]].end(), back_inserter(inter));
                }
                if (!inter.size()) {
                    printf("NO\n");
                    continue;
                }
                set<string> ans;
                for (vector <int> ::iterator p = inter.begin(); p < inter.end(); ++p) {
                    //       printf("%d ^^^*\n", *p);
                    ans.insert(totip[*p]);
                }
                set<string> ::iterator u;
                u = ans.begin();
                cout << *u;
                for (++u; u != ans.end(); ++u)
                    cout << " " << *u;
                printf("\n");
            }
        }
        return 0;
    }
    这条路我们走的太匆忙~拥抱着并不真实的欲望~
  • 相关阅读:
    RabbitMQ——CLI 管理工具 rabbitmqadmin
    RabbitMQ——Fedration插件
    RabbitMQ——使用Shovel插件迁移数据
    Http——HttpClient使用
    Http——Keep-Alive机制
    Http——HttpURLConnection详解
    Kafak——日志数据策略
    Keepalived + LVS 搭建高可用负载均衡
    TCP——抓包分析
    常见的网络端口
  • 原文地址:https://www.cnblogs.com/baidongtan/p/2796620.html
Copyright © 2011-2022 走看看