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();
int len;
string ig;
while(getline(cin,ig) && ig!="::")
ignore.insert(ig);
mp.clear();
while(getline(cin,tmp))
{
len=tmp.length();
for(int i=;i<len;++i)
tmp[i]=tolower(tmp[i]);
string t1;
int cnt;
for(int i=;i<len;++i)
{
if(tmp[i]!=' ')
{
cnt=;
int j;
t1.clear();
for(j=i;j<len;++j)
{
if(tmp[j]!=' ')
{
t1.insert(cnt,,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=;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 ;
}
/* */

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[][];
void fun(char *str)
{
int len=strlen(str);
for(int i=;i<len;i++) str[i]=tolower(str[i]); n=;
char *strPtr=strtok(str," ");
while(strPtr!=NULL)
{
strcpy(word[n++],strPtr);
strPtr=strtok(NULL," ");
}
}
}title[];
void output(int t,int pos)
{
int len=strlen(title[t].word[pos]);
for(int i=;i<len;i++)
{
title[t].word[pos][i]=toupper(title[t].word[pos][i]);
}
for(int i=;i<title[t].n;i++)
{
if(i==)
{
printf("%s",title[t].word[i]);
}
else printf(" %s",title[t].word[i]);
}
for(int i=;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();
int n=;
set<string> ig,key;
set<string>::iterator it;
char temp[],str[];
while(scanf("%s",temp)!=EOF)
{
if(strcmp(temp,"::")==) break;
ig.insert(temp);
}
while(gets(str))
{
title[n].fun(str);
for(int i=;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())==)
{
flag=true;break;
}
}
if(!flag) key.insert(title[n].word[i]);
}
n++;
}
for(it=key.begin();it!=key.end();it++)
{
for(int i=;i<n;i++)
{
for(int j=;j<title[i].n;j++)
{
if(strcmp((*it).c_str(),title[i].word[j])==)
{
output(i,j);
}
}
}
}
return ;
}
/* */

STL --- UVA 123 Searching Quickly的更多相关文章

  1. uva 123 Searching Quickly

     Searching Quickly  Background Searching and sorting are part of the theory and practice of computer ...

  2. Brute Force & STL --- UVA 146 ID Codes

     ID Codes  Problem's Link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&a ...

  3. STL UVA 11995 I Can Guess the Data Structure!

    题目传送门 题意:训练指南P186 分析:主要为了熟悉STL中的stack,queue,priority_queue,尤其是优先队列从小到大的写法 #include <bits/stdc++.h ...

  4. UVa 12505 Searching in sqrt(n)

    传送门 一开始在vjudge上看到这题时,标的来源是CSU 1120,第八届湖南省赛D题“平方根大搜索”.今天交题时CSU突然跪了,后来查了一下看哪家OJ还挂了这道题,竟然发现这题是出自UVA的,而且 ...

  5. uva 1597 Searching the Web

    The word "search engine" may not be strange to you. Generally speaking, a search engine se ...

  6. 湖南省第八届大学生程序设计大赛原题 D - 平方根大搜索 UVA 12505 - Searching in sqrt(n)

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=30746#problem/D D - 平方根大搜索 UVA12505 - Searchin ...

  7. STL UVA 11991 Easy Problem from Rujia Liu?

    题目传送门 题意:训练指南P187 分析:用vector存id下标,可以用map,也可以离散化用数组存(发现不用离散化也可以) map #include <bits/stdc++.h> u ...

  8. UVa123 - Searching Quickly

    题目地址:点击打开链接 C++代码: #include <iostream> #include <set> #include <map> #include < ...

  9. [STL] UVA 10815 安迪的第一个字典 Andy's First Dictionary

    1.set 集合 哦....对了,set有自动按照字典序排序功能..... 声明和插入操作 #include <cstdio> #include <vector> #inclu ...

随机推荐

  1. jquery.jqzoom.js图片放大镜

    jqzoom插件实现图片放大镜效果 1. jquery.jqzoom.js //************************************************************ ...

  2. python数据结构之图深度优先和广度优先

    首先有一个概念:回溯 回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步重新选择,这种走不通就退回再走的技术为回溯法 ...

  3. c++ bind1st 和 bind2nd的用法

    std::bind1st 和 std::bind2nd将二元函数转换为一元函数,具体用法参加下面的代码. 代码介绍了两种使用方式,第一种是使用std::less和std::greater,第二种是使用 ...

  4. iOS 实现快速切换主题详细教程(附上源码)

    前言 iOS 实现主题切换,相信在未来的app里也是会频繁出现的,尽管现在只是出现在主流的APP,如(QQ.新浪微博.酷狗音乐.网易云音乐等),但是现在是看颜值.追求个性的年代,所以根据用户喜好自定义 ...

  5. Fedora 手动删除系统中不再需要的包

    最新文章:Virson‘s Blog 1.安装yum-utils yum install yum-utils 2.使用package-cleanup --leaves列举系统中不再需要的包 packa ...

  6. Windows 8.1 Preview 开发资源汇总

    Microsoft Build 2013开发者大会已经结束,从Session安排上看主要以Windows 8.1为主.我相信大家有已经或多或少的体验过Windows 8.1 Preview了,关于操作 ...

  7. nginx server_name

    在我的机子了nginx的  server_name要配制成127.0.0.1才能用,否则就报错,刚试用nginx还不知道为什么,先记下来

  8. ping: sendto: Network is unreachable

    在我的板子上ping路由上的IP的时候可以ping通,但是ping外网的IP的时候提示"ping: sendto: Network is unreachable" 后来使用rout ...

  9. 一种高效的 vector 四则运算处理方法

    实现 vector 的四则运算 这里假设 vector 的运算定义为对操作数 vector 中相同位置的元素进行运算,最后得到一个新的 vector.具体来说就是,假如 vector<int&g ...

  10. ruby -- 进阶学习(十四)设置background-image(解决无法获取图片路径问题)

    基于rails4.0环境 为了美化界面,添加背景图片,于是又傻逼了一回~~ 一开始在xxx.html.erb中添加:(注:图片的路径为:app/asssets/images/background.jp ...