POJ 2162 Document Indexing(模拟)
Description
- If the last line on a page is the last line of the paragraph, then the following empty line is skipped, i.e. it is not placed on any page. Therefore, the page never starts with a blank line.
- If the last line on a page is the first line of a paragraph that contains more than one line (so called orphan line), then it is moved to the next page.
- If the last line on a page is the next-to-last line of a paragraph that contains more than three lines, then this line is moved to the next page (otherwise, the last line of the paragraph would be alone on the page -- so called widow line).
- If the last line on a page is the next-to-last line of a paragraph that contains exactly two or three lines, then the whole paragraph is moved to the next page (so we have neither orphan, nor widow lines).
After applying the correction rules the next page is formed, and so on until the whole document is paginated. A word is a continuous sequence of letters of the English alphabet. Case is not important. The index of the document contains each word from the document and the list of the pages it occurs at. The numbers of pages a word occurs at must be listed in the ascending order. Numbers must be separated by commas. If a word occurs on three or more consecutive pages, only the first and the last page numbers of this range must be listed, separated by a dash, for example "3-5,7-10,12,13,15".
Input
Output
题目大意:模拟一些段落的书页分配。除了一段只有一行的,不要让任何行单独在一页的最上面和最下面。
思路:模拟。注意如果像我这么做一段一段读的话要开大内存,之前开了1000行结果WA了无数次>_<。我的做法相当暴力啊o(╯□╰)o
代码(922MS):
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cctype>
#include <map>
#include <cstring>
#include <string>
using namespace std;
typedef long long LL; const int MAXN = ; map<string, int> mymap;
char s[][MAXN];
bool ans[][];
int n, page, row, cur, cnt; string to_str(char *&st) {
while(!isalpha(*st) && *st != ) ++st;
string ret;
while(isalpha(*st) && *st != ) {
if(islower(*st)) *st += 'A' - 'a';
ret += *st, ++st;
}
return ret;
} void to_map(char *s) {
string tmp;
while(true) {
tmp = to_str(s);
if(tmp == "") break;
int now;
if(mymap.find(tmp) != mymap.end()) now = mymap[tmp];
else mymap[tmp] = now = ++cnt;
//cout<<tmp<<endl;
ans[now][page] = true;
}
} void output() {
map<string, int>::iterator it;
for(it = mymap.begin(); it != mymap.end(); ++it) {
bool flag = false;
int now = it->second;
cout<<it->first;
for(int i = ; i <= page; ++i) {
if(!ans[now][i]) continue;
if(!flag) putchar(' '), flag = true;
else putchar(',');
printf("%d", i);
int j = i;
while(ans[now][j + ]) ++j;
if(j >= i + ) {
printf("-%d", j);
i = j;
}
}
puts("");
}
} int main() {
scanf("%d", &n); getchar();
page = , row = ;
cur = ; cnt = ;
bool flag = true;
mymap.clear();
while(flag && gets(s[])) {
cur = ;
while((flag = gets(s[cur])) && s[cur][] != ) ++cur;
if(cur == ) {
to_map(s[]);
++row;
if(++row > n) row = , ++page;
continue;
}
if(cur == ) {
if(row == n) row = , ++page;
to_map(s[]);
to_map(s[]);
row += ;
if(++row > n) row = , ++page;
continue;
}
if(cur == ) {
if(row + == n || row == n) row = , ++page;
to_map(s[]);
to_map(s[]);
to_map(s[]);
row += ;
if(++row > n) row = , ++page;
continue;
}
if(row == n) row = , ++page;//cur >= 4
for(int i = ; i < cur; ++i) {
if(row == n && i == cur - ) row = , ++page;
to_map(s[i]);
++row;
if(row > n) row = , ++page;
}
if(row == ) continue;
if(++row > n) row = , ++page;
}
output();
}
POJ 2162 Document Indexing(模拟)的更多相关文章
- Codeforces Round #375 (Div. 2) B. Text Document Analysis 模拟
B. Text Document Analysis 题目连接: http://codeforces.com/contest/723/problem/B Description Modern text ...
- HDU 2494/POJ 3930 Elevator(模拟)(2008 Asia Regional Beijing)
Description Too worrying about the house price bubble, poor Mike sold his house and rent an apartmen ...
- poj 2632 Crashing Robots 模拟
题目链接: http://poj.org/problem?id=2632 题目描述: 有一个B*A的厂库,分布了n个机器人,机器人编号1~n.我们知道刚开始时全部机器人的位置和朝向,我们可以按顺序操控 ...
- POJ 2014 Flow Layout 模拟
http://poj.org/problem?id=2014 嘻嘻2014要到啦,于是去做Prob.ID 为2014的题~~~~祝大家新年快乐~~ 题目大意: 给你一个最大宽度的矩形,要求把小矩形排放 ...
- POJ 2632 Crashing Robots (模拟 坐标调整)(fflush导致RE)
题目链接:http://poj.org/problem?id=2632 先话说昨天顺利1Y之后,直到今天下午才再出题 TAT,真是刷题计划深似海,从此AC是路人- - 本来2632是道略微恶心点的模拟 ...
- poj 1888 Crossword Answers 模拟题
Crossword Answers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 869 Accepted: 405 D ...
- POJ 2632 Crashing Robots 模拟 难度:0
http://poj.org/problem?id=2632 #include<cstdio> #include <cstring> #include <algorith ...
- poj 3253 Fence Repair(模拟huffman树 + 优先队列)
题意:如果要切断一个长度为a的木条需要花费代价a, 问要切出要求的n个木条所需的最小代价. 思路:模拟huffman树,每次选取最小的两个数加入结果,再将这两个数的和加入队列. 注意priority_ ...
- Code POJ - 1780(栈模拟dfs)
题意: 就是数位哈密顿回路 解析: 是就算了...尼玛还不能直接用dfs,得手动开栈模拟dfs emm...看了老大半天才看的一知半解 #include <iostream> #inclu ...
随机推荐
- 菜鸟笔记 -- Chapter 1 计算机从0到1
进入20世纪第二个十年,计算机已经成为生活中一个必不可小的工具了,但我们真的了解计算机吗?计算机有哪些部分构成?不同的计算机又可以做什么样的事情呢?我们的PC和用来做加减乘除的计算器都属于计算机范畴吗 ...
- Spring的声明式事务----Annotation注解方式(1)
这里列一个小的demo工程,直接利用Spring的jdbcTemplate访问Mysql数据库. 工程结构: 数据库中的tbl_student表结构如下: 数据实体类Student.java代码如下: ...
- Vscode配置C++运行环境(2019/1//11更)并加入bits/stdc++.h头文件
因为重装系统,以前配置好的c++环境又没了.所以有要配置一遍. 1 下载mingw64或minw, 配置好环境变量:C://mingw64//bin; 在cmd下用g++ -v验证是否成功. 2. ...
- MHA实现mysql高可用复制集群
MHA简述 MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,是一套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用软件.在My ...
- 分别使用原生js和jQuery添加/删除元素的class属性
一.原生js添加/删除元素的class属性: <!-- span元素原有class = "test" --> <span class="test&quo ...
- 深度解析JQuery Dom元素操作技巧
深度解析JQuery Dom元素操作技巧 DOM是一种与浏览器.平台.语言无关的接口,使用该接口可以轻松访问页面中所有的标准组件,这篇文章给大家介绍了JQuery dom元素操作方法,写的十分的全面细 ...
- JavaScript之原型 Prototype
1.我们所创建的每一个函数,解析器都会向函数中添加一个属性prototype.这个属性对应着一个对象,这个对象就是我们所谓的原型对象.如果函索作为普通函数调用prototype没有任何作用. 当函数以 ...
- mybatis 打印SQL
如果使用的是application.properties文件,加入如下配置: #打印SQL logging.level.com.jn.ssr.supererscuereporting.dao=debu ...
- 匈牙利算法模板 hdu 1150 Machine Schedule(二分匹配)
二分图:https://blog.csdn.net/c20180630/article/details/70175814 https://blog.csdn.net/flynn_curry/artic ...
- 20145202 《Java程序设计》实验五实验报告
一.实验内容 1.用书上的TCP代码,实现服务器与客户端. 2.客户端与服务器连接 3.客户端中输入明文,利用DES算法加密,DES的秘钥用RSA公钥密码中服务器的公钥加密,计算明文的Hash函数值, ...