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文本排序(根据关键字的字典)。

 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

 

目录
相关文章
|
2天前
Strange fuction(HDU--2899)
Strange fuction(HDU--2899)
|
9月前
UVa11958 - Coming Home
UVa11958 - Coming Home
28 0
|
9月前
UVa11565 - Simple Equations
UVa11565 - Simple Equations
36 0
|
9月前
uva101 The Blocks Problem
uva101 The Blocks Problem
34 0
|
Java C++
HDU 1004 Let the Balloon Rise【STL<map>】
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 123800    Accepted Submi...
1512 0