该课题来源于UVA中Searching the Web的题目:https://vjudge.net/problem/UVA-1597

按照题目的说法,我对按照特定格式输入的文章中的词语合成字典,以满足后期的快速查找。

针对于字典的合成途径,我利用了STL中的map与set的嵌套形成了一种特定的数据结构来解析文章中的单词

 #include<map>
#include<iostream>
#include<set>
#include<algorithm>
#include<string>
#include<cctype>
#include<sstream>
using namespace std;
struct newpair
{
int article;
int line;
bool operator<(const newpair b) const
{
return this->line < b.line;
}
};
typedef map<string,set<newpair> > BIGMAP;
typedef set<newpair>::iterator SET_pair_ITER;
typedef map<string,set<newpair> >::iterator BIGMAP_iter; BIGMAP maper;
string psd[];
int maxline; int checkmaper()
{
BIGMAP_iter it;
for(it=maper.begin();it!=maper.end();++it)
{
cout<<(it->first);//string-type
set<newpair> cyc;
cyc=it->second;//set<newpair>-type
for(SET_pair_ITER iter=cyc.begin();iter!=cyc.end();++iter)
{
newpair ctn=*iter;
cout<<" article "<<ctn.article<<" line "<<ctn.line<<endl;
}
}
return ;
} void buildmaper(string aim,int articlenum,int linenum)
{
newpair m;
m.article=articlenum;
m.line=linenum;
maper[aim].insert(m);
} int readin()
{
int n;
char c;//input the \n
cin>>n>>c;
int cur=;
for(int i=;i<n;cur++)
{
getline(cin,psd[cur]);
if((int)psd[cur].find("***")!=-){i++;continue;}//the next article
for(string::iterator it=psd[cur].begin();it!=psd[cur].end();++it)
{
if(isalpha(*it)) *it=tolower(*it);
else *it=' ';
}
stringstream ss(psd[cur]);
string chr;
while(ss>>chr) buildmaper(chr,i,cur);
}
return cur;
} int main()
{
freopen("input.txt","r",stdin);
freopen("ans.txt","w",stdout);
maxline=readin();
checkmaper();
return ;
}

以上代码涉及了较多C++知识与个别底层知识,下面进行列举:

1、stringstream常用操作

2、基本STL之map与set

3、结构体中的运算符重载

4、迭代器的操作

5、RB树实现map与set的基本原理

有关详细的实现方法请参照我的其它博客和上述代码。

在上述代码中唯一一个容易出现bug的位置是set的实现:由于set对输入的元素需要进行排序,所以必须在newpair结构体中重载<(operator)。

下面是运行图片:

输入如下:

one   repeat  repeat  repeat
A manufacturer, importer, or seller of
digital media devices may not () sell,
or offer for sale, in interstate commerce,
or () cause to be transported in, or in a
manner affecting, interstate commerce,
a digital media device unless the device
includes and utilizes standard security
technologies that adhere to the security
system standards.
**********
one two repeat repeat repeat repeat
Of course, Lisa did not necessarily
intend to read his books. She might
want the computer only to write her
midterm. But Dan knew she came from
a middle-class family and could hardly
afford the tuition, let alone her reading
fees. Books might be the only way she
could graduate
**********
one two three repeat repeat repeat repeat repeat
Research in analysis (i.e., the evaluation
of the strengths and weaknesses of
computer system) is essential to the
development of effective security, both
for works protected by copyright law
and for information in general. Such
research can progress only through the
open publication and exchange of
complete scientific results
**********
one two three four repeat repeat repeat repeat repeat repeat
I am very very very happy!
What about you?
**********

输出如下:

a  article  line
article line
article line
article line
about article line
adhere article line
affecting article line
afford article line
alone article line
am article line
analysis article line
and article line
article line
article line
article line
article line
be article line
article line
books article line
article line
both article line
but article line
by article line
came article line
can article line
cause article line
class article line
commerce article line
article line
complete article line
computer article line
article line
copyright article line
could article line
article line
course article line
dan article line
development article line
device article line
devices article line
did article line
digital article line
article line
e article line
effective article line
essential article line
evaluation article line
exchange article line
family article line
fees article line
for article line
article line
article line
four article line
from article line
general article line
graduate article line
happy article line
hardly article line
her article line
article line 17 其余略。。。。。。。。。。

OK

最新文章

  1. C++STL学习笔记_(1)deque双端数组知识
  2. u3d单词学习plane
  3. 无废话ExtJs 入门教程四[表单:FormPanel]
  4. Linux就这个范儿 第13章 打通任督二脉
  5. [原创]java WEB学习笔记52:国际化 fmt 标签,国际化的总结
  6. iOS中 视频直播功能-流媒体的使用(详解)韩俊强的CSDN博客
  7. 每天学一点JAVA
  8. poj 1651 http://poj.org/problem?id=1651
  9. springMVC整合xStream
  10. EF结合SqlBulkCopy在项目中的使用
  11. Visual Studio vs2010 去掉中文注释红色下划线;去掉代码红色下划线;
  12. SPI协议总结
  13. 谁说程序员都是苦逼的——看看兄弟连上海S2班的点点滴滴
  14. Deeplearning.ai课程笔记--汇总
  15. Vim的合并行操作
  16. [EXP]Microsoft Windows 10 - XmlDocument Insecure Sharing Privilege Escalation
  17. java通过接口扩展枚举
  18. js, javascript 图片懒加载 实例代码
  19. centos7永久更改主机名
  20. vc2015编译paho.mqtt.c-1.1.0

热门文章

  1. 第3章 JDK并发包(一)
  2. C++:析构函数的调用时机
  3. HTTP下帐号密码的截取
  4. 前端项目引入Echarts中的dataTool的正确方式
  5. C# 一个帮您理解回调函数的例子(新手必看)
  6. JavaScript-状态模式
  7. 移动端rem.js
  8. ts中的接口
  9. Hystrix压测
  10. 使用BeanUtils.populate将map集合封装为bean对象