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文本排序(根据关键字的字典)。
注意:一行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; } /* */
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; } /* */
作者:北岛知寒
出处:https://www.cnblogs.com/crazyacking/p/4474810.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
2014-05-03 codeforces --- Round #244 (Div. 2) A. Police Recruits
2014-05-03 线段树 --- (区间维护+逆推)
2014-05-03 线段数 --- (单点更新、求逆序对)
2014-05-03 线段树 --- (单点更新、区间求和、模板题)