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. git submodule update --init --recursive

    最近在跑好几个模型,视频检测,物体检测,搭建mxnet时,有点问题,记录一下. 视频检测,mxnet需要用指定版本,git 切换到指定版本后,update了,但是依然提示说有些库找不到.想了想,应该是 ...

  2. linux 使用sqlite3

    :c中使用sqlite3需要调用函数接口操作: sqlite3 *db; int status=sqlite_open("dbname",&db);//打开或者创建数据库 ...

  3. vue2高仿饿了么app

    Github地址: https://github.com/ccyinghua/appEleme-project 一.构建项目所用: vue init webpack appEleme-project ...

  4. POJ2406 Power Strings(KMP)

    Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 56162   Accepted: 23370 Description Giv ...

  5. MySQL实现序列自增

    #创建序列表 DROP TABLE IF EXISTS `sequence`; CREATE TABLE `sequence` ( `name` ) NOT NULL COMMENT '序列名称', ...

  6. Flask模拟实现CSRF攻击

    CSRF CSRF全拼为Cross Site Request Forgery,译为跨站请求伪造. CSRF指攻击者盗用了你的身份,以你的名义发送恶意请求. 包括:以你名义发送邮件,发消息,盗取你的账号 ...

  7. mybatis报错:查询一对多或多对多时只返回一条数据的问题

    问题: 使用映射文件实现查询一对多或多对多时只返回一条数据问题 解决方法: 导致这种情况出现的问题是因为两个表中的主键是一样所以出现了数据覆盖问题. 解决方式一:修改数据库表中的主键(这种方法比较麻烦 ...

  8. 分享一个工作中遇得到的sql(按每天每人统计拖车次数与小修次数)

    查询每人每天的数据 首先先建表 CREATE TABLE `user` ( `name` ) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CR ...

  9. 利用nodejs实现商品管理系统(一)

    一.界面分类:用户登录界面,商品管理界面(包含商品编辑,创建,删除,列表界面) 功能实现:1.用户输入用户名与密码,通过加密,与数据库校验,如果正确,则跳转到商品管理界面,否则一直停留在用户界面. 2 ...

  10. HBase 是什么

    Apache HBase™ is the Hadoop database, a distributed, scalable, big data store. HBase 是 Hadoop databa ...