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. Python 学习笔记(七)Python字符串(一)

    字符串 字符串或串(String)是由数字.字母.下划线组成的一串字符,用双引号或单引号包裹的为字符串 1 >>> "hello world" 2 'hello ...

  2. rest_framework -- mixins&generics

    上面的mixins.generics都是rest_framework里的模块,我们可以继承其中的某些类,达到代码量减少的效果,这里充分体现出了面向对象的继承 一.mixins模块 mixins : f ...

  3. SI - 硬件 - 服务器 - 知识科普

    服务器对每个从事IT工作的人来说并不陌生,但是服务器所涉及的各种知识细节,并非大家都十分清楚,为了让大家深入了解服务器的关键知识点,笔者特意抽时间总结了这篇科普文章,旨在帮助读者全面了解服务器.今天内 ...

  4. java程序执行命令行,解锁数据库表

    有些表锁的时间长或其他原因,在plsql中不能解锁,只能用命令行解锁. 有些功能跨平台系统的交互偶尔会锁表,就需要自动解锁. 下面是解锁的代码: package com.lg.BreakOracleU ...

  5. Thinkphp5所有页面验证用户是否登陆

    新建Base.php控制器,所有的页面继承自它 <?php namespace app\index\controller; use think\Controller; class Base ex ...

  6. Hadoop(7)-HDFS客户端的API操作

    1 客户端环境准备 根据自己电脑的操作系统拷贝对应的编译后的hadoop jar包到非中文路径 配置HADOOP_HOME的环境变量,并且在path中配置hadoop的bin 重启电脑 2. Hdfs ...

  7. Ubuntu装完后要做的几件事

    Ubuntu装完后要做的几件事 改hosts 无论哪里,改hosts都是第一件事,没hosts咋google.没google咋活.在终端输入命令 sudo gedit /etc/hosts在# The ...

  8. C语言学习记录_2019.02.02

    变量在第一次被使用之前应该赋初值 scanf(“%d”,&price); scanf(“price%d %d”,&price);  scanf中的东西一定是要输入的东西. 定义常量:c ...

  9. dotnet core 数据库

    dotnet core 数据库 程序开发过程中,需要使用数据对数据进行存储,分析等.通常而言都会使用ORM来实现关系数据库与实体对象的转化,过使用描述对象和数据库之间映射的元数据,将程序中的对象自动持 ...

  10. (数据科学学习手札04)Python与R在自定义函数上的异同

    自编函数是几乎每一种编程语言的基础功能,有些时候我们需要解决的问题可能没有完全一致的包中的函数来进行解决,这个时候自编函数就成了一样利器,而Python与R在这方面也有着一定的差别,下面举例说明: P ...