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. .net core 1.0 实现负载多服务器单点登录

    前言 .net core 出来有一时间了,这段时间也一直在做技术准备,目前想做一个单点登录(SSO)系统,在这之前用.net时我用习惯了machineKey ,也顺手在.net core 中尝试了一上 ...

  2. Tomcat8配置数据库连接池

    1.所有的tomcat项目共用一个连接池配置 1.1 修改conf->context.xml文件,在Context节点下配置 <Resource name="jdbc/myDat ...

  3. EF: Returns multi table from procedure

    原文:https://msdn.microsoft.com/en-us/data/jj691402.aspx

  4. Error occurred during initialization of VM java/lang/NoClassDefFoundError: java/lang/Object

    本地原来已经安装了JAVA JDK1.7并配置好了环境变量; 然后又安装了JDK8,想2个版本并存. 然后发现eclipse 打不开,闪退.然后查看环境: 发现 C:\Users\Administra ...

  5. 在android studio中导入github下载的工程

    1.从Github中下载工程压缩包,并将其解压到本地 2.修改文件 假设,解压后的文件目录如下: (1)修改配置文件  xx\build.gradle // Top-level build file ...

  6. 将数据库表导入到solr索引

    将数据库表导入到solr索引 编辑solrcofnig.xml添加处理器 <requestHandler name="/dataimport" class="org ...

  7. 《热血传奇2》wix、wil文件解析Java实现

    在百度上搜索java+wil只有iteye上一篇有丁点儿内容,不过他说的是错的!或者说是不完整的,我个人认为我对于热血传奇客户端解析还是有一定研究的,请移步: <JMir——Java版热血传奇2 ...

  8. Android 自定义Drawable

    1.使用BitmapShader实现图片圆角 public class CornerDrawable extends Drawable { private Paint mPaint; private ...

  9. python 字符串截取

    我们可以通过索引来提取想要获取的字符,可以把python的字符串也做为字符串的列表就更好理解 python的字串列表有2种取值顺序1是从左到右索引默认0开始的,最大范围是字符串长度少1s = 'ilo ...

  10. 十八、【开源】EnterpriseFrameWork框架核心类库之Winform控制器

    回<[开源]EnterpriseFrameWork框架系列文章索引> EFW框架源代码下载:http://pan.baidu.com/s/1qWJjo3U EFW框架中的WinContro ...