Description

Andy is fond of old computers. He loves everything about them and he uses emulators of old operating systems on his modern computer. Andy also likes writing programs for them. Recently he has decided to write a text editor for his favorite text-mode operating system.  The most difficult task he has got stuck with is document indexing. An index of the document is the lexicographically ordered list of all words occurring in the document with the numbers of pages they occur at. Andy feels that he is not able to write the component of the editor that performs indexing, so he asks you to help.  A document is a sequence of paragraphs. Each paragraph consists of one or more lines. Paragraphs are separated from each other with exactly one blank line.  First, the document is paginated -- divided into pages. Each page consists of up to n lines. Lines are placed on the page one after another, until n lines are placed. The following correction rules are then applied:

  • 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

The first line of the input contains n (4 <= n <= 100). The rest of the input file contains the document to be indexed. The size of the input does not exceed 20 000 bytes.  The line is considered blank if it is completely empty. No line contains leading or trailing spaces. The document does not contain two consecutive blank lines. The first line of the document is not blank. The length of each line of the document does not exceed 200 characters.

Output

Print all words that occur in the given document. Words must be printed in the lexicographical order, one word on a line. After each word print one space followed by the list of pages it occurs at, formatted as described in problem statement. Use capital letters in 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(模拟)的更多相关文章

  1. Codeforces Round #375 (Div. 2) B. Text Document Analysis 模拟

    B. Text Document Analysis 题目连接: http://codeforces.com/contest/723/problem/B Description Modern text ...

  2. 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 ...

  3. poj 2632 Crashing Robots 模拟

    题目链接: http://poj.org/problem?id=2632 题目描述: 有一个B*A的厂库,分布了n个机器人,机器人编号1~n.我们知道刚开始时全部机器人的位置和朝向,我们可以按顺序操控 ...

  4. POJ 2014 Flow Layout 模拟

    http://poj.org/problem?id=2014 嘻嘻2014要到啦,于是去做Prob.ID 为2014的题~~~~祝大家新年快乐~~ 题目大意: 给你一个最大宽度的矩形,要求把小矩形排放 ...

  5. POJ 2632 Crashing Robots (模拟 坐标调整)(fflush导致RE)

    题目链接:http://poj.org/problem?id=2632 先话说昨天顺利1Y之后,直到今天下午才再出题 TAT,真是刷题计划深似海,从此AC是路人- - 本来2632是道略微恶心点的模拟 ...

  6. poj 1888 Crossword Answers 模拟题

    Crossword Answers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 869   Accepted: 405 D ...

  7. POJ 2632 Crashing Robots 模拟 难度:0

    http://poj.org/problem?id=2632 #include<cstdio> #include <cstring> #include <algorith ...

  8. poj 3253 Fence Repair(模拟huffman树 + 优先队列)

    题意:如果要切断一个长度为a的木条需要花费代价a, 问要切出要求的n个木条所需的最小代价. 思路:模拟huffman树,每次选取最小的两个数加入结果,再将这两个数的和加入队列. 注意priority_ ...

  9. Code POJ - 1780(栈模拟dfs)

    题意: 就是数位哈密顿回路 解析: 是就算了...尼玛还不能直接用dfs,得手动开栈模拟dfs emm...看了老大半天才看的一知半解 #include <iostream> #inclu ...

随机推荐

  1. springboot jar 部署到linux之后 获取类资源文件问题-- 仅限linux 下 情况比较特殊 需要获取打到jar内的 讲台资源 只能通过流获取,根据路径获取不到指定文件 nullpointExption

    https://blog.csdn.net/qq_27000425/article/details/72897282 ClassPathResource类,如果没有指定相对的类名,该类将从类的根路径开 ...

  2. jsp页面的传值(popup)

    jsp页面与xml文件对应的关系: 例:网页上jsp的url为----purchase_app_btn.do? 对应xml文件下的 <action path="/purchase_ap ...

  3. 前端关于SEO

    提高页面加载速度. 能用css解决的不用背景图片,背景图片也尽量压缩大小,可以几个icons放在一个图片上,使用background-position找到需要的图片位置.可以减少HTTP请求数,提高网 ...

  4. UML绘制活动图--客户来电咨询活动图

    选择Logic View–>New–>Activity Diagram 修改NewActivity为客户来电咨询 选择初始状态和终止状态(下图中上面是Start State,下面是End ...

  5. 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛--I-填空题

    链接:https://www.nowcoder.com/acm/contest/90/I 来源:牛客网 1.题目描述 牛客网是是一个专注于程序员的学习和成长的专业平台,集笔面试系统.课程教育.社群交流 ...

  6. 转:Zookeeper客户端Curator使用详解

    原文:https://www.jianshu.com/p/70151fc0ef5d Zookeeper客户端Curator使用详解 前提 最近刚好用到了zookeeper,做了一个基于SpringBo ...

  7. python 如何在列表list,字典dict,集合set 中根据条件筛选数据

    from random import randint """ list 过滤掉负数 """ data = [randint(-10, 10) ...

  8. SI - 系统 - 操作系统简述 (Operating System)

    Unix 操作系统:System V.BSD Microsoft Windows Apple Mac OS Linux FreeBSD 安装 https://jingyan.baidu.com/art ...

  9. js函数的节流和防抖

    js函数的节流和防抖 用户浏览页面时会不可避免的触发一些高频度触发事件(例如页面 scroll ,屏幕 resize,监听用户输入等),这些事件会频繁触发浏览器的重拍(reflow)和重绘(repai ...

  10. JDK6 新特性

    JDK6新特性目录导航: Desktop类和SystemTray类 JAXB2实现对象与XML之间的映射 StAX Compiler API 轻量级 Http Server API 插入式注解处理AP ...