博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2162 Document Indexing(模拟)
阅读量:4983 次
发布时间:2019-06-12

本文共 5483 字,大约阅读时间需要 18 分钟。

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):

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 using namespace std; 11 typedef long long LL; 12 13 const int MAXN = 1010; 14 15 map
mymap; 16 char s[20000][MAXN]; 17 bool ans[20000][10000]; 18 int n, page, row, cur, cnt; 19 20 string to_str(char *&st) { 21 while(!isalpha(*st) && *st != 0) ++st; 22 string ret; 23 while(isalpha(*st) && *st != 0) { 24 if(islower(*st)) *st += 'A' - 'a'; 25 ret += *st, ++st; 26 } 27 return ret; 28 } 29 30 void to_map(char *s) { 31 string tmp; 32 while(true) { 33 tmp = to_str(s); 34 if(tmp == "") break; 35 int now; 36 if(mymap.find(tmp) != mymap.end()) now = mymap[tmp]; 37 else mymap[tmp] = now = ++cnt; 38 //cout<
<
::iterator it; 45 for(it = mymap.begin(); it != mymap.end(); ++it) { 46 bool flag = false; 47 int now = it->second; 48 cout<
first; 49 for(int i = 1; i <= page; ++i) { 50 if(!ans[now][i]) continue; 51 if(!flag) putchar(' '), flag = true; 52 else putchar(','); 53 printf("%d", i); 54 int j = i; 55 while(ans[now][j + 1]) ++j; 56 if(j >= i + 2) { 57 printf("-%d", j); 58 i = j; 59 } 60 } 61 puts(""); 62 } 63 } 64 65 int main() { 66 scanf("%d", &n); getchar(); 67 page = 1, row = 1; 68 cur = 1; cnt = 0; 69 bool flag = true; 70 mymap.clear(); 71 while(flag && gets(s[0])) { 72 cur = 1; 73 while((flag = gets(s[cur])) && s[cur][0] != 0) ++cur; 74 if(cur == 1) { 75 to_map(s[0]); 76 ++row; 77 if(++row > n) row = 1, ++page; 78 continue; 79 } 80 if(cur == 2) { 81 if(row == n) row = 1, ++page; 82 to_map(s[0]); 83 to_map(s[1]); 84 row += 2; 85 if(++row > n) row = 1, ++page; 86 continue; 87 } 88 if(cur == 3) { 89 if(row + 1 == n || row == n) row = 1, ++page; 90 to_map(s[0]); 91 to_map(s[1]); 92 to_map(s[2]); 93 row += 3; 94 if(++row > n) row = 1, ++page; 95 continue; 96 } 97 if(row == n) row = 1, ++page;//cur >= 4 98 for(int i = 0; i < cur; ++i) { 99 if(row == n && i == cur - 2) row = 1, ++page;100 to_map(s[i]);101 ++row;102 if(row > n) row = 1, ++page;103 }104 if(row == 1) continue;105 if(++row > n) row = 1, ++page;106 }107 output();108 }
View Code

 

转载于:https://www.cnblogs.com/oyking/p/3293391.html

你可能感兴趣的文章
hdu 4284 状态压缩
查看>>
逆向分析技术
查看>>
记开发过的一款无线音箱解决方案
查看>>
Latex
查看>>
SpringMVC处理JSON
查看>>
几何建模
查看>>
java crm 系统 进销存 springmvc SSM项目项目源码
查看>>
jQuery.extend 函数详解
查看>>
<jQuery> 一. jQuery简介及优点
查看>>
架构相关概念——学习笔记
查看>>
被称为“开发者神器”的GitHub,到底该怎么用?
查看>>
(坑集)Django环境配置
查看>>
利用padding-top/padding-bottom百分比,进行占位和高度自适应
查看>>
常用的监控系统资源的工具
查看>>
08ssm三大框架整合以前步骤
查看>>
R语言学习笔记之八
查看>>
正则表达式语法(msdn)
查看>>
oralce使用INSERT语句向表中插入数据
查看>>
MySQL 数据类型 详解 (转载)
查看>>
干净win7要做几步才能运行第一个Spring MVC 写的动态web程序
查看>>