[刷题]算法竞赛入门经典(第2版) 5-10/UVa1597 - Searching the Web
题意:不难理解,照搬题意的解法。
代码:(Accepted,0.190s)
//UVa1597 - Searching the Web
//#define _XIENAOBAN_
#include<iostream>
#include<sstream>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
using namespace std;
map<string, set<unsigned> > word;
char lines[1510][85];
unsigned N, M, la[105], ln(1);//la:last line of the article; ln:line number
char tmpword[80], tmpline[88], w1[80], w2[80], w3[80];
void ONLY(bool& flag, const string& on) {
auto &t(word[on]);
auto p(t.begin());
for (unsigned i(1);i <= N;++i) {
while (p != t.end() && *p < la[i - 1]) ++p;
if (p != t.end() && *p < la[i]) {
if (flag) puts("----------");
else flag = true;
while (p != t.end() && *p < la[i])
puts(lines[*p++]);
}
}
}
void NOT(bool& flag, const string& on) {
auto &t(word[on]);
auto p(t.begin());
for (unsigned i(0);i < N;++i) {
while (p != t.end() && *p < la[i]) ++p;
if (p == t.end() || *p >= la[i + 1]) {
if (flag) puts("----------");
else flag = true;
for (auto j(la[i]);j < la[i + 1];++j)
puts(lines[j]);
}
}
}
void AND(bool& flag, const string& le, const string& ri) {
auto &t1(word[le]), &t2(word[ri]);
auto p1(t1.begin()),
p2(t2.begin());
for (unsigned i(1);i <= N;++i) {
while (p1 != t1.end() && *p1 < la[i - 1]) ++p1;
while (p2 != t2.end() && *p2 < la[i - 1]) ++p2;
if ((p1 != t1.end() && *p1 < la[i]) &&
(p2 != t2.end() && *p2 < la[i])) {
if (flag) puts("----------");
else flag = true;
unsigned re;
while ((p1 != t1.end() && *p1 < la[i]) ||
(p2 != t2.end() && *p2 < la[i])) {
if (p1 == t1.end() || *p1 >= la[i]) re = *p2++;
else if (p2 == t2.end() || *p2 >= la[i]) re = *p1++;
else if (*p2 > *p1) re = *p1++;
else if (*p2 < *p1) re = *p2++;
else re = *p1++, *p2++;
puts(lines[re]);
}
}
}
}
void OR(bool& flag, const string& le, const string& ri) {
auto p1(word[le].begin()),
p2(word[ri].begin());
for (unsigned i(1);i <= N;++i) {
while (p1 != word[le].end() && *p1 < la[i - 1]) ++p1;
while (p2 != word[ri].end() && *p2 < la[i - 1]) ++p2;
if ((p1 != word[le].end() && *p1 < la[i]) ||
(p2 != word[ri].end() && *p2 < la[i])) {
if (flag) puts("----------");
else flag = true;
unsigned re;
while ((p1 != word[le].end() && *p1 < la[i]) ||
(p2 != word[ri].end() && *p2 < la[i])) {
if (p1 == word[le].end() || *p1 >= la[i]) re = *p2++;
else if (p2 == word[ri].end() || *p2 >= la[i]) re = *p1++;
else if (*p2 > *p1) re = *p1++;
else if (*p2 < *p1) re = *p2++;
else re = *p1++, *p2++;
puts(lines[re]);
}
}
}
}
int main()
{
#ifdef _XIENAOBAN_
#define gets(T) gets_s(T, 88)
freopen("in.txt", "r", stdin);
#endif
scanf("%d", &N);
getchar();
for (unsigned now(0);now < N;++now) { //read contexts of articles
la[now] = ln;
while (gets(tmpline) && strcmp(tmpline, "**********")) {
strcpy(lines[ln], tmpline);
char *pl(tmpline);
do {
if ((*pl >= 'a' && *pl <= 'z') || (*pl >= 'A' && *pl <= 'Z')) {
char *pw(tmpword);
while ((*pl >= 'a' && *pl <= 'z') || (*pl >= 'A' && *pl <= 'Z')) {
if (*pl >= 'A' && *pl <= 'Z') *pl += 32;
*pw++ = *pl++;
}
*pw = 0;
word[tmpword].insert(ln);
}
} while (*pl++ != 0);
++ln;
}
}
la[N] = ln;
scanf("%d", &M);
getchar();
int nnnn = 0;
while (M--) { //read requests
gets(tmpline);
char *p = tmpline, *pw1 = w1, *pw2 = w2, *pw3 = w3;
while (*p && *p != ' ') *pw1++ = *p++;
*pw1 = '\0';
if (*p) ++p;
while (*p && *p != ' ') *pw2++ = *p++;
*pw2 = '\0';
if (*p) ++p;
while (*p && *p != ' ') *pw3++ = *p++;
*pw3 = '\0';
bool flag(false);
if (!*w2) ONLY(flag, w1);
else if (*w1 == 'N') NOT(flag, w2);
else if (*w2 == 'A') AND(flag, w1, w3);
else OR(flag, w1, w3);
if (!flag) puts("Sorry, I found nothing.");
puts("==========");
//printf("==========%d\n", ++nnnn);//
}
return 0;
}
分析:我选择死亡。。。难倒是不难,很快就解出来了。但是一开始用了近1秒的时间,很郁闷。于是又花了几天的时间去研究别人的代码,也包括把之前sstream全改掉用gets和puts,把only not and or全部分开做函数,写的老长。用gets和puts的版本用了0.600s,还是比别人的都慢好多。GitHub上看到大神0.080s的(https://github.com/morris821028/UVa/blob/master/temp/1597%20-%20Searching%20the%20Web.cpp),我天这是差了多少倍!于是仿着大神的代码也写了个,结果1.090s!!??一定是我没抄到其精髓?!花了几天时间了,结果还变慢了,好难过啊。。。然后过了几天我又回来研究了。我发现其实网上看到的算法全都是大同小异的,那我的代码一定是哪里有问题。看到大神函数里面的这一句:set<int> &t = wordInLine[args[1]]; 一开始我以他只是图个书写方便,但想了想,这样就不需要每次寻找wordInLine[args[1]]的值了,于是我就抱着试试的心态,也加了句类似的。结果瞬间从0.960s降到0.190s!新技能get!
下面是仅仅没有给word[on]、word[le]、word[ri]加引用的版本。
代码:(Accepted,1.090s)
//UVa1597 - Searching the Web
//#define _XIENAOBAN_
#include<iostream>
#include<sstream>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
using namespace std;
map<string, set<unsigned> > word;
char lines[1510][85];
unsigned N, M, la[105], ln(1);//la:last line of the article; ln:line number
char tmpword[80], tmpline[88], w1[80], w2[80], w3[80];
void ONLY(bool& flag, const string& on) {
auto p(word[on].begin());
for (unsigned i(1);i <= N;++i) {
while (p != word[on].end() && *p < la[i - 1]) ++p;
if (p != word[on].end() && *p < la[i]) {
if (flag) puts("----------");
else flag = true;
while (p != word[on].end() && *p < la[i])
puts(lines[*p++]);
}
}
}
void NOT(bool& flag, const string& on) {
auto p(word[on].begin());
for (unsigned i(0);i < N;++i) {
while (p != word[on].end() && *p < la[i]) ++p;
if (p == word[on].end() || *p >= la[i + 1]) {
if (flag) puts("----------");
else flag = true;
for (auto j(la[i]);j < la[i + 1];++j)
puts(lines[j]);
}
}
}
void AND(bool& flag, const string& le, const string& ri) {
auto p1(word[le].begin()),
p2(word[ri].begin());
for (unsigned i(1);i <= N;++i) {
while (p1 != word[le].end() && *p1 < la[i - 1]) ++p1;
while (p2 != word[ri].end() && *p2 < la[i - 1]) ++p2;
if ((p1 != word[le].end() && *p1 < la[i]) &&
(p2 != word[ri].end() && *p2 < la[i])) {
if (flag) puts("----------");
else flag = true;
unsigned re;
while ((p1 != word[le].end() && *p1 < la[i]) ||
(p2 != word[ri].end() && *p2 < la[i])) {
if (p1 == word[le].end() || *p1 >= la[i]) re = *p2++;
else if (p2 == word[ri].end() || *p2 >= la[i]) re = *p1++;
else if (*p2 > *p1) re = *p1++;
else if (*p2 < *p1) re = *p2++;
else re = *p1++, *p2++;
puts(lines[re]);
}
}
}
}
void OR(bool& flag, const string& le, const string& ri) {
auto p1(word[le].begin()),
p2(word[ri].begin());
for (unsigned i(1);i <= N;++i) {
while (p1 != word[le].end() && *p1 < la[i - 1]) ++p1;
while (p2 != word[ri].end() && *p2 < la[i - 1]) ++p2;
if ((p1 != word[le].end() && *p1 < la[i]) ||
(p2 != word[ri].end() && *p2 < la[i])) {
if (flag) puts("----------");
else flag = true;
unsigned re;
while ((p1 != word[le].end() && *p1 < la[i]) ||
(p2 != word[ri].end() && *p2 < la[i])) {
if (p1 == word[le].end() || *p1 >= la[i]) re = *p2++;
else if (p2 == word[ri].end() || *p2 >= la[i]) re = *p1++;
else if (*p2 > *p1) re = *p1++;
else if (*p2 < *p1) re = *p2++;
else re = *p1++, *p2++;
puts(lines[re]);
}
}
}
}
int main()
{
#ifdef _XIENAOBAN_
#define gets(T) gets_s(T, 88)
freopen("in.txt", "r", stdin);
#endif
scanf("%d", &N);
getchar();
for (unsigned now(0);now < N;++now) { //read contexts of articles
la[now] = ln;
while (gets(tmpline) && strcmp(tmpline, "**********")) {
strcpy(lines[ln], tmpline);
char *pl(tmpline);
do {
if ((*pl >= 'a' && *pl <= 'z') || (*pl >= 'A' && *pl <= 'Z')) {
char *pw(tmpword);
while ((*pl >= 'a' && *pl <= 'z') || (*pl >= 'A' && *pl <= 'Z')) {
if (*pl >= 'A' && *pl <= 'Z') *pl += 32;
*pw++ = *pl++;
}
*pw = 0;
word[tmpword].insert(ln);
}
} while (*pl++ != 0);
++ln;
}
}
la[N] = ln;
scanf("%d", &M);
getchar();
int nnnn = 0;
while (M--) { //read requests
gets(tmpline);
char *p = tmpline, *pw1 = w1, *pw2 = w2, *pw3 = w3;
while (*p && *p != ' ') *pw1++ = *p++;
*pw1 = '\0';
if (*p) ++p;
while (*p && *p != ' ') *pw2++ = *p++;
*pw2 = '\0';
if (*p) ++p;
while (*p && *p != ' ') *pw3++ = *p++;
*pw3 = '\0';
bool flag(false);
if (!*w2) ONLY(flag, w1);
else if (*w1 == 'N') NOT(flag, w2);
else if (*w2 == 'A') AND(flag, w1, w3);
else OR(flag, w1, w3);
if (!flag) puts("Sorry, I found nothing.");
puts("==========");
//printf("==========%d\n", ++nnnn);//
}
return 0;
}
哎呀呀,就少那么几行,天壤之别。顺便把一开始做的版本也贴了:
代码:(Accepted,0.960s)
//UVa1597 - Searching the Web
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<map>
#include<set>
using namespace std;
struct article {
map<string, vector<unsigned> > word;
vector<string> line;
};
const article tmpart;//an empty article,just for push_back
unsigned N, M;
string tmpstr;
vector<article> dat;
inline bool ONLY(const article& ar, const string& l, const string& r) {
return ar.word.count(l);
}
inline bool AND(const article& ar, const string& l, const string& r) {
return ar.word.count(l) && ar.word.count(r);
}
inline bool OR(const article& ar, const string& l, const string& r) {
return ar.word.count(l) || ar.word.count(r);
}
void NOT(const string& on) {
bool flag(false);
for (const auto& r : dat) {
if (!r.word.count(on)) {
if (flag) cout << "----------\n";
else flag = true;
for (const auto& rr : r.line)
cout << rr << '\n';
}
}
if (!flag) cout << "Sorry, I found nothing.\n";
}
void solve(bool(*f)(const article&, const string&, const string&), const string& le, const string& ri) {
bool flag(false);
for (auto& r : dat) {//why it can't be "const auto& r"? because if a map is const,r.word[i] would be invalid
if (f(r, le, ri)) {
set<unsigned> re;
if (r.word.count(le)) for (const auto& rr : r.word[le]) re.insert(rr);
if (r.word.count(ri)) for (const auto& rr : r.word[ri]) re.insert(rr);
if (re.empty()) continue;
if (flag) cout << "----------\n";
else flag = true;
for (const auto& rr : re)
cout << r.line[rr] << '\n';
}
}
if (!flag) cout << "Sorry, I found nothing.\n";
}
int main()
{
//freopen("in.txt", "r", stdin);//
std::ios::sync_with_stdio(false);
cin >> N;
cin.ignore();//absorb '\n'
for (unsigned now(0);now < N;++now) { //read contexts of articles
int ln(0);//line number of current article
dat.push_back(tmpart);//create a empty article
while (getline(cin, tmpstr) && tmpstr != "**********") {
dat[now].line.push_back(tmpstr);
for (auto& r : tmpstr) {
if (r >= 'a'&&r <= 'z') continue;
if (r >= 'A'&&r <= 'Z') r += 32;
else r = ' ';
}
istringstream in(tmpstr);
while (in >> tmpstr)
dat[now].word[tmpstr].push_back(ln);
++ln;
}
}
cin >> M;
cin.ignore();
while (M--) { //read requests
string sl, sm, sr;//left middle right
getline(cin, tmpstr);
istringstream in(tmpstr);
in >> sl;
if (!(in >> sm)) solve(ONLY, sl, sl);
else if (!(in >> sr)) NOT(sm);//why are you so special
else if (sm[0] == 'A') solve(AND, sl, sr);
else solve(OR, sl, sr);
cout << "==========\n";
}
return 0;
}
[刷题]算法竞赛入门经典(第2版) 5-10/UVa1597 - Searching the Web的更多相关文章
- [刷题]算法竞赛入门经典(第2版) 4-6/UVa508 - Morse Mismatches
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,10 ms) //UVa508 - Morse Mismatches #include< ...
- [刷题]算法竞赛入门经典(第2版) 5-15/UVa12333 - Revenge of Fibonacci
题意:在前100000个Fibonacci(以下简称F)数字里,能否在这100000个F里找出以某些数字作为开头的F.要求找出下标最小的.没找到输出-1. 代码:(Accepted,0.250s) / ...
- [刷题]算法竞赛入门经典(第2版) 5-13/UVa822 - Queue and A
题意:模拟客服MM,一共有N种话题,每个客服MM支持处理其中的i个(i < N),处理的话题还有优先级.为了简化流程方便出题,设每个话题都是每隔m分钟来咨询一次.现知道每个话题前来咨询的时间.间 ...
- [刷题]算法竞赛入门经典(第2版) 4-5/UVa1590 - IP Networks
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) //UVa1590 - IP Networks #include<iost ...
- [刷题]算法竞赛入门经典(第2版) 6-7/UVa804 - Petri Net Simulation
题意:模拟Petri网的执行.虽然没听说过Petri网,但是题目描述的很清晰. 代码:(Accepted,0.210s) //UVa804 - Petri Net Simulation //Accep ...
- [刷题]算法竞赛入门经典(第2版) 6-6/UVa12166 - Equilibrium Mobile
题意:二叉树代表使得平衡天平,修改最少值使之平衡. 代码:(Accepted,0.030s) //UVa12166 - Equilibrium Mobile //Accepted 0.030s //# ...
- [刷题]算法竞赛入门经典(第2版) 6-1/UVa673 6-2/UVa712 6-3/UVa536
这三题比较简单,只放代码了. 题目:6-1 UVa673 - Parentheses Balance //UVa673 - Parentheses Balance //Accepted 0.000s ...
- [刷题]算法竞赛入门经典(第2版) 5-16/UVa212 - Use of Hospital Facilities
题意:模拟患者做手术. 其条件为:医院有Nop个手术室.准备手术室要Mop分钟,另有Nre个恢复用的床.准备每张床要Mre分钟,早上Ts点整医院开张,从手术室手术完毕转移到回复床要Mtr分钟.现在医院 ...
- [刷题]算法竞赛入门经典(第2版) 5-11/UVa12504 - Updating a Dictionary
题意:对比新老字典的区别:内容多了.少了还是修改了. 代码:(Accepted,0.000s) //UVa12504 - Updating a Dictionary //#define _XieNao ...
随机推荐
- CF615D Multipliers [数学]
tags:[计数原理][乘法逆元][归纳の思想]题解(复杂度:O(mlogm)):棘手之处:n的约数多到爆炸.因此我们不妨从因子的角度来分析问题.对n分解质因数得:n = p1^a1 * p2^a2 ...
- Linux网络那点事
跨平台系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#linux 之前的之前说过网络自连接的配置(CentOS服务器网络配置:http://ww ...
- 老李分享:loadrunner操作mysql数据库
老李分享:loadrunner操作mysql数据库 在poptest测试开发工程师就业培训的课程中,针对一套商业系统进行性能测试,目标是mysql后台数据库的负载能力,在这里我把测试代码 ...
- 在centOS7.2里安装virtualenv和flask
1) 安装pip工具 #wget https://bootstrap.pypa.io/get-pip.py #python get-pip.py 2) 安装virtualenv,并创建一个开发环境 # ...
- my first blogs(我的处女博)
末夏的夕阳送走一批批下班的人,些许的轻风给一天烦躁的心带来一丝丝的清凉.我倒是挺喜欢在这种天气,提前下了公交车然后漫步回家.这样我能多点时间回顾一天的事情,俗话说是思考人生. 不知不觉毕业两年多了,在 ...
- 设计模式的征途—2.简单工厂(Simple Factory)模式
工厂模式是最常用的一种创建型模式,通常所说的工厂模式一般是指工厂方法模式.本篇是是工厂方法模式的“小弟”,我们可以将其理解为工厂方法模式的预备知识,它不属于GoF 23种设计模式,但在软件开发中却也应 ...
- day002-HTML知识点总结:浏览器兼容性之指定IE浏览器使用chrome内核渲染页面
今天再浏览大淘宝首页时,突然看到这么一个东东: ,顿时好费解,莫非万恶的IE浏览器认识到自己以往的罪孽,开始兼容chrome了??! 于是本着不懂就百度的神精,开始纵横于各大铁耙,勃哥,终于找到了许许 ...
- python_原始_web框架
创:10_4_2017 修: 什么是web框架? -- 本质上是socket,用户请求来,业务逻辑处理,返回处理结果 -- 包含socket或者不包含socket的框架 什么是wsgi? -- web ...
- Angular2入门-数据绑定
▓▓▓▓▓▓ 大致介绍 Angular2中数据绑定的方式默认是以单向方式,数据绑定的方式可以分为: 1.属性绑定和插值表达式 组件类-> 模板 2.事件绑定:模板 -> 组件类 3.双向绑 ...
- android设备使用usb串口传输数据
首先介绍两个开源项目一个是Google的开源项目:https://code.google.com/archive/p/android-serialport-api/ 另一个是我们这次介绍的开源项目:h ...