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. string的+操作与StringBuilder对象

    习惯在C#代码中写str+="xxx";这样代码的请注意啦,如果这种操作是针对单个变量作很多次叠加操作的,很有可能导致性能降低. 大家都知道string与StringBuilder ...

  2. 突然顿悟的Javascript中的this

    一直对Javascript中的this都有一种似是而非的感觉,今天突然感觉豁然开朗,特此记录一下. 咱们先看个栗子: <!DOCTYPE html> <html> <he ...

  3. 关于MYSQL中like 检索汉字问题。

    mysql中like’中国‘查询不出来结果 如果 like‘zg’就看查询出来结果. 查询了半天是由于mysql中编码的原因 解决办法: 进入mysql 输入命令:show variables lik ...

  4. Mac 上面使用SVN的攻略

    参考网站: http://blog.csdn.net/q199109106q/article/details/8655204 SVN命令集: http://www.blogjava.net/jasmi ...

  5. Swift 自动布局框架-SnapKit

    官方网址:http://snapkit.io/ Github: https://github.com/SnapKit/SnapKit SnapKit is a DSL to make Auto Lay ...

  6. 找回Win8.1(windows server 2012 R2)的双拼

    一.微软拼音的选项,只能在metro界面修改: 鼠标移动到屏幕右下,出现metro的菜单,选"设置"然后选最下面的"更改电脑设置" 时间和设置->区域和语 ...

  7. Nginx upstream 长连接

    原文: http://bollaxu.iteye.com/blog/900424 Nginx upstream目前只有短连接,通过HTTP/1.0向后端发起连接,并把请求的"Connecti ...

  8. Java与邮件系统交互之使用Socket验证邮箱是否存在

    最近遇到一个需求:需要验证用户填写的邮箱地址是否真实存在,是否可达.和普通的正则表达式不同,他要求尝试链接目标邮箱服务器并请求校验目标邮箱是否存在. 先来了解 DNS之MX记录 对于DNS不了解的,请 ...

  9. Apache2.4中开通HTTP基本认证

    Apache2.4中开通HTTP基本认证,需要在Apache的配置文件中增加如下代码 WSGIPassAuthorization On 否则则无法认证

  10. 15个实用的CSS在线实例教程

    前端技术可以说是必须学习的一个技术,现在做网站都需要懂DIV.CSS,在国内很多企业招网页设计师都要求会写基本的前端代码,所以前端技术是 必须了解的,对网页设计师本身也有帮助,今天向大家推荐一些不错的 ...