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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-04
  • 2021-09-21
  • 2022-12-23
  • 2021-07-07
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-23
  • 2021-11-26
  • 2021-12-27
  • 2021-10-22
  • 2022-12-23
相关资源
相似解决方案