zoukankan      html  css  js  c++  java
  • STL --- UVA 123 Searching Quickly

     UVA - 123 Searching Quickly

    Problem's Link:   http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19296


    Mean: 

    有一个字符串集合Ignore,还有一个文本集合TXT,在TXT中除了Ignore中的单词外其他的都是关键字,现在要你根据这些关键字来给TXT文本排序(根据关键字的字典)。

    注意:一行TXT文本中含多少个关键字就需要排多少次序,如果关键字的字典序相同则按照先后顺序来排。

    analyse:

    这题STL用的比较多,使用STL可以很好的解决去重、排序等一序列问题,而手动实现的话就稍微繁琐一点,思路并不难。

    Time complexity: O(n)

    Source code: 

    1. STL版:

    /*
    * this code is made by crazyacking
    * Problem: UVA 123
    * Verdict: Accepted
    * Submission Date: 2015-05-03-20.39
    * Time: 0MS
    * Memory: 0KB
    */
    #include <queue>
    #include <cstdio>
    #include <string>
    #include <stack>
    #include <cmath>
    #include <set>
    #include <map>
    #include <cstdlib>
    #include <climits>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #define  MAXN 1000010
    #define  LL long long
    #define  ULL unsigned long long
    using namespace std;
    
    string tmp;
    set<string> ignore;
    multimap<string,string> mp;
    
    int main()
    {
    //        freopen("C:\Users\crazyacking\Desktop\cin.txt","r",stdin);
    //        freopen("C:\Users\crazyacking\Desktop\cout.txt","w",stdout);
    
            ios_base::sync_with_stdio(false);
            cin.tie(0);
            int len;
            string ig;
            while(getline(cin,ig) && ig!="::")
                    ignore.insert(ig);
            mp.clear();
            while(getline(cin,tmp))
            {
                    len=tmp.length();
                    for(int i=0;i<len;++i)
                            tmp[i]=tolower(tmp[i]);
                    string t1;
                    int cnt;
                    for(int i=0;i<len;++i)
                    {
                            if(tmp[i]!=' ')
                            {
                                    cnt=0;
                                    int j;
                                    t1.clear();
                                    for(j=i;j<len;++j)
                                    {
                                            if(tmp[j]!=' ')
                                            {
                                                    t1.insert(cnt,1,tmp[j]);
                                                    cnt++;
                                                    tmp[j]=toupper(tmp[j]);
                                            }
                                            else break;
                                    }
                                    i=j;
                                    if(ignore.find(t1)==ignore.end())
                                            mp.insert(pair<string,string>(t1,tmp));
                                    for(j=0;j<len;++j)
                                    {
                                            tmp[j]=tolower(tmp[j]);
                                    }
                            }
                    }
            }
            multimap<string,string> ::iterator iter=mp.begin();
            for(;iter!=mp.end();++iter)
            {
                    cout<<(iter->second)<<endl;
            }
            return 0;
    }
    /*
    
    */
    View Code

    2.手动模拟:

    /*
    * this code is made by crazyacking
    * Verdict: Accepted
    * Submission Date: 2015-05-03-21.52
    * Time: 0MS
    * Memory: 1347KB
    */
    #include <queue>
    #include <cstdio>
    #include <string>
    #include <stack>
    #include <cmath>
    #include <set>
    #include <map>
    #include <cstdlib>
    #include <climits>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #define  MAXN 1000010
    #define  LL long long
    using namespace std;
    
    struct node
    {
        int n;
        char word[20][1500];
        void fun(char *str)
        {
            int len=strlen(str);
            for(int i=0;i<len;i++) str[i]=tolower(str[i]);
    
            n=0;
            char *strPtr=strtok(str," ");
            while(strPtr!=NULL)
            {
                strcpy(word[n++],strPtr);
                strPtr=strtok(NULL," ");
            }
        }
    }title[300];
    void output(int t,int pos)
    {
        int len=strlen(title[t].word[pos]);
        for(int i=0;i<len;i++)
        {
            title[t].word[pos][i]=toupper(title[t].word[pos][i]);
        }
        for(int i=0;i<title[t].n;i++)
        {
            if(i==0)
            {
                printf("%s",title[t].word[i]);
            }
            else printf(" %s",title[t].word[i]);
        }
        for(int i=0;i<len;i++)
        {
            title[t].word[pos][i]=tolower(title[t].word[pos][i]);
        }
        puts("");
    }
    
    int main()
    {
            ios_base::sync_with_stdio(false);
            cin.tie(0);
            int n=0;
                set<string> ig,key;
                set<string>::iterator it;
                char temp[20],str[10005];
                while(scanf("%s",temp)!=EOF)
                {
                    if(strcmp(temp,"::")==0) break;
                    ig.insert(temp);
                }
                while(gets(str))
                {
                    title[n].fun(str);
                    for(int i=0;i<title[n].n;i++)
                    {
                        bool flag=false;
                        for(it=ig.begin();it!=ig.end();it++)
                        {
                            if(strcmp(title[n].word[i],(*it).c_str())==0)
                            {
                                flag=true;break;
                            }
                        }
                        if(!flag) key.insert(title[n].word[i]);
                    }
                    n++;
                }
                for(it=key.begin();it!=key.end();it++)
                {
                    for(int i=0;i<n;i++)
                    {
                        for(int j=0;j<title[i].n;j++)
                        {
                            if(strcmp((*it).c_str(),title[i].word[j])==0)
                            {
                                output(i,j);
                            }
                        }
                    }
                }
            return 0;
    }
    /*
    
    */
    View Code
  • 相关阅读:
    转发:原本优秀的我们是怎样滑向平庸的
    阿里巴巴C++实习生相关招聘一则
    [置顶] 我希望在20岁时就知道的26条时间管理技巧
    提示用户进行版本更新并且发布通知监控版本下载情况
    树状数组求正序数与逆序数hdu Minimum Inversion Number
    通过js 实现简单验证码的 刷新 看不清 换一张
    配置 CACTI 监控 MySQL 数据库状态
    Win7下ADB不能识别设备的可能原因
    Java 开源博客——B3log Solo 0.6.0 正式版发布了!
    MyEclipse下Import没有Maven的处理办法
  • 原文地址:https://www.cnblogs.com/crazyacking/p/4474810.html
Copyright © 2011-2022 走看看