ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛 The Book List

描述

The history of Peking University Library is as long as the history of Peking University. It was build in 1898. At the end of year 2015, it had about 11,000 thousand volumes of books, among which 8,000 thousand volumes were paper books and the others were digital ones. Chairman Mao Zedong worked in Peking University Library for a few months as an assistant during 1918 to 1919. He earned 8 Dayang per month there, while the salary of top professors in Peking University is about 280 Dayang per month.

Now Han Meimei just takes the position which Chairman Mao used to be in Peking University Library. Her first job is to rearrange a list of books. Every entry in the list is in the format shown below:

CATEGORY 1/CATEGORY 2/..../CATEGORY n/BOOKNAME

It means that the book BOOKNAME belongs to CATEGORY n, and CATEGORY n belongs to CATEGORY n-1, and CATEGORY n-1 belongs to CATEGORY n-2...... Each book belongs to some categories. Let's call CATEGORY1  "first class category", and CATEGORY 2 "second class category", ...ect. This is an example:

MATH/GRAPH THEORY
ART/HISTORY/JAPANESE HISTORY/JAPANESE ACIENT HISTORY
ART/HISTORY/CHINESE HISTORY/THREE KINDOM/RESEARCHES ON LIUBEI
ART/HISTORY/CHINESE HISTORY/CHINESE MORDEN HISTORY
ART/HISTORY/CHINESE HISTORY/THREE KINDOM/RESEARCHES ON CAOCAO

Han Meimei needs to make a new list on which the relationship between books and the categories is shown by indents. The rules are:

1) The n-th class category has an indent of  4×(n-1) spaces before it.
2) The book directly belongs to the n-th class category has an indent of  4×n spaces before it.
3) The categories and books which directly belong to a category X should be list below X in dictionary order. But all categories go before all books.
4) All first class categories are also list by dictionary order.

For example, the book list above should be changed into the new list shown below:

ART
    HISTORY
        CHINESE HISTORY
            THREE KINDOM
                RESEARCHES ON CAOCAO
                RESEARCHES ON LIUBEI
            CHINESE MORDEN HISTORY
        JAPANESE HISTORY
            JAPANESE ACIENT HISTORY
MATH
    GRAPH THEORY

Please help Han Meimei to write a program to deal with her job.

输入

There are no more than 10 test cases.
Each case is a list of no more than 30 books, ending by a line of "0". 
The description of a book contains only uppercase letters, digits, '/' and spaces, and it's no more than 100 characters.
Please note that, a same book may be listed more than once in the original list, but in the new list, each book only can be listed once. If two books have the same name but belong to different categories, they are different books.

输出

For each test case, print "Case n:" first(n starts from 1), then print the new list as required.

样例输入

B/A
B/A
B/B
0
A1/B1/B32/B7
A1/B/B2/B4/C5
A1/B1/B2/B6/C5
A1/B1/B2/B5
A1/B1/B2/B1
A1/B3/B2
A3/B1
A0/A1
0

样例输出

Case 1:
B
    A
    B
Case 2:
A0
    A1
A1
    B
        B2
            B4
                C5
    B1
        B2
            B6
                C5
            B1
            B5
        B32
            B7
    B3
        B2
A3
    B1
只有代码,题解。。。
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define pb push_back
#define fi first
#define se second
#define icc(x) (1<<(x))
#define lcc(x) (1ll<<(x))
#define lowbit(x) (x&-x)
#define debug(x) cout<<#x<<"="<=s;--i)
#define mset(g, x) memset(g, x, sizeof(g))
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef double db;
typedef pair pii;
typedef pair pll;
typedef vector veci;
const int mod=(int)1e9+7,inf=0x3fffffff,rx[]={-1,0,1,0},ry[]={0,1,0,-1};
const ll INF=1ll<<60;
const db pi=acos(-1),eps=1e-8;

template void rd(T &res){
    res = 0; int ch,sign=0;
    while( (ch=getchar())!='-' && !(ch>='0'&&ch<='9'));
    if(ch == '-') sign = 1; else res = ch-'0';
    while((ch=getchar())>='0'&&ch<='9') res = (res<<3)+(res<<1)+ch-'0';
    res = sign?-res:res;
}
templatevoid rec_pt(T x){
    if(!x)return;
    rec_pt(x/10);
    putchar(x%10^48);
}
templatevoid pt(T x){
    if(x<0) putchar('-'),x=-x;
    if(!x)putchar('0');
    else rec_pt(x);
}
templateinline void ptn(T x){ pt(x),putchar('\n'); }
templateinline void Max(T &a,T b){ if(b>a)a=b; }
templateinline void Min(T &a,T b){ if(binline T mgcd(T b,T d){ return b?mgcd(d%b,b):d; }//gcd模板,传入的参数必须是用一类型
//-------------------------------主代码--------------------------------------//

char str[33][1100];
string ss[33];;

int main()
{
    int tt=1;
    int cnt=0;
    while(gets(str[cnt])){
        if(str[cnt][0]=='0' &&strlen(str[cnt])==1){
            printf("Case %d:\n",tt++);
            rep(i, 0, cnt){
                ss[i]="";
                int prej = 0;
                rep(j, 0, strlen(str[i])){
                    if(str[i][j]==' ') str[i][j]='&';
                    
                    if(str[i][j]=='/'){
                        string tmp= "%";
                        
                        rep(k, prej, j){
                            tmp += str[i][k];
                        }
                        ss[i] += tmp;
                        ss[i] += "!";
                        prej = j+1;
                    }
                }
                rep(j, prej, strlen(str[i])){
                    ss[i] += str[i][j];
                }
            }
            sort(ss,ss+cnt);
            rep(i, 0, cnt){
                if(i==0){
                    int n = 0;
                    rep(j, 0, ss[i].length()){
                        if(ss[i][j] == '%')continue;
                        if(ss[i][j] == '&'){ printf(" "); continue;}
                        if(ss[i][j] == '!'){
                            n++;
                            puts("");
                            rep(k, 0, 4*n){
                                putchar(' ');
                            }
                        }else printf("%c",ss[i][j]);
                    }
                    //puts("");
                    //if(cnt!=1) puts("");
                    continue;
                }
                if(ss[i]==ss[i-1]) continue;
                int pp=0;
                int n=0;
                rep(j, 0, ss[i-1].length()){
                    
                    if(ss[i][j] != ss[i-1][j]){
                        break;
                    }
                    if(ss[i][j] == '!'){
                        n++;
                        pp = j+1;
                    }
                }
                //while(pp>0 && ss[i][pp]!='!') pp--;
                puts("");
                rep(j, 0, 4*n) putchar(' ');
                
                rep(j, pp, ss[i].length()){
                    if(ss[i][j] == '%')continue;
                    if(ss[i][j] == '&'){ printf(" "); continue;}
                    if(ss[i][j] == '!'){
                        n++;
                        puts("");
                        rep(k, 0, 4*n){
                            putchar(' ');
                        }
                    }else printf("%c",ss[i][j]);
                }
                
            }
            cnt = 0;
            printf("\n");
            //tt++;
        }else{
            cnt++;
        }
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/yinghualuowu/p/5903630.html

你可能感兴趣的:(ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛 The Book List)