UVA230解题报告
来源:互联网 发布:禁锢网络剧百度云 编辑:程序博客网 时间:2024/05/17 06:32
这个题耗了我六天时间,很打击我对算法的学习,不过,我终于解决了他。分析如下
仔细观察我们可以发现后面的操作与输出都是围绕标题(title)展开的,作者(author)所起的作用不过是排序而已。我们还注意到一个有趣的地方,即书籍上架时实现排序在上架,那么我们完全可以认为如果能确定一本书的状态的话,我们直接从前往后扫过去就可以了。状态的确定难不难呢?我觉得不难,由题意知每本书有三种状态(已上架、借出、归还但未上架),所以我们可以在定义个变量state记录这三种状态,分别用1、-1、0表示。状态与作者属性都封装在结构体book里,用map与书名形成映射。
书籍上架还有个重要的点要解决,那就是确定书籍的位置。我们可以用扫描法,从当前书籍往前扫,如果有书则输出在这本书后,没书也即j<0时输出该书是第一本书
附上AC代码Time 0ms
#include<cstdio>#include<cstring>#include<vector>#include<map>#include<sstream>#include<algorithm>#include<iostream>using namespace std;typedef struct book{ int state; string author;}book;const int maxn=200;char info[maxn];vector<string> libs;map<string,book> m;bool cmp(string s1,string s2){ if(m[s1].author!=m[s2].author) return m[s1].author<m[s2].author; else return s1<s2;}void add(){ stringstream ss(info); string title="",author=""; string buf; ss>>buf; title+=buf; while(ss>>buf){ if(buf=="by") break; title+=" "; title+=buf; } libs.push_back(title); ss>>buf; author+=buf; while(ss>>buf){ author+=" "; author+=buf; } book b; b.state=1; b.author=author; m[title]=b;}void operate(){ string order,title,buf; stringstream ss(info); ss>>order; if(ss>>buf) title+=buf; while(ss>>buf) { title+=" "; title+=buf; } switch(order[0]) { case 'B':{ m[title].state=-1; break; } case 'R' :{ m[title].state=0; break; } case 'S':{ for(int i=0;i<libs.size();i++) { title=libs[i]; if(m[title].state!=0) continue; int j; for(j=i-1;j>=0;j--) if(m[libs[j]].state==1) break; if(j<0) cout<<"Put "<<title<<" first"<<endl; else cout<<"Put "<<title<<" after "<<libs[j]<<endl; m[title].state=1; } printf("END\n"); break; } }}int main(){ while(gets(info) && info[0]!='E' ) add(); sort(libs.begin(),libs.end(),cmp); while(gets(info) && info[0]!='E') operate(); return 0;}
阅读全文
1 0
- UVA230解题报告
- Uva230
- UVa230
- 解题报告
- 解题报告
- 解题报告
- 解题报告
- 解题报告
- 解题报告
- 解题报告
- UVa230 - Borrowers
- UVA230 - Borrowers
- UVA230 Borrowers
- UVa230 Borrowers
- uva230-Borrowers
- Antiprime解题报告
- expr解题报告
- 华容道解题报告
- Python博客总结4
- poj 2104 <排序分块,区间第k大>/<第一次用主席树>2个方法
- Redis单机版与集群版的搭建
- 越简单越好:看看新QNX技术概念车--使用rightware的kanziUI设计
- USACO-Section 1.2 Daul Palindromes[...]
- UVA230解题报告
- 增加收入的 6 种方式
- 第34课: Stage划分和Task最佳位置算法源码彻底解密
- C#两个日期控件的比较,出现奇怪的现象
- Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
- 通达OA二次开发 小飞鱼应用OA工作流二次开发智能公文系统(二)(图文)
- Magento2开发文档教程
- Matrix Decompressing UVA
- Magento开发手册教程