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

 

目录
相关文章
uva 11991 - Easy Problem from Rujia Liu?
这个题目的意思是输入n个数,m组询问,每组询问包含两个整数k,v,意思是询问整数v第k次出现的位置。
41 0
UVa1531 - Problem Bee
UVa1531 - Problem Bee
51 0
UVa12478 - Hardest Problem Ever (枚举)
UVa12478 - Hardest Problem Ever (枚举)
46 0
LeetCode 383. Ransom Note
给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串ransom能不能由第二个字符串magazines里面的字符构成。如果可以构成,返回 true ;否则返回 false。
70 0
LeetCode 383. Ransom Note
LeetCode之Ransom Note
LeetCode之Ransom Note
114 0
|
Java
[LeetCode] Ransom Note
A very typical application of hash maps. Since I am now learning Java, I code in Java. The following code uses toCharArray() and getOrDefault(), which are learnt from this post.
916 0
[UVa OJ] Longest Common Subsequence
This is the classic LCS problem. Since it only requires you to print the maximum length, the code can be optimized to use only O(m) space (see here).
776 0