zoukankan      html  css  js  c++  java
  • POJ 2162 Document Indexing(模拟)

    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 <cstdio>
      2 #include <cstring>
      3 #include <iostream>
      4 #include <algorithm>
      5 #include <queue>
      6 #include <cctype>
      7 #include <map>
      8 #include <cstring>
      9 #include <string>
     10 using namespace std;
     11 typedef long long LL;
     12 
     13 const int MAXN = 1010;
     14 
     15 map<string, int> 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<<tmp<<endl;
     39         ans[now][page] = true;
     40     }
     41 }
     42 
     43 void output() {
     44     map<string, int>::iterator it;
     45     for(it = mymap.begin(); it != mymap.end(); ++it) {
     46         bool flag = false;
     47         int now = it->second;
     48         cout<<it->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
  • 相关阅读:
    委托&指针函数&回调函数
    Unity animation笔记1
    hadoop源码编译
    protocbuf的安装
    学习hadoop不错的一些文章
    moven的安装
    在Linux上安装与配置Hadoop
    linux tar命令详解
    How to contribute to hadoop common
    Ubuntu下SVN的安装
  • 原文地址:https://www.cnblogs.com/oyking/p/3293391.html
Copyright © 2011-2022 走看看