1. 程式人生 > >hihoCoder - 1871 Heshen's Account Book (2018ICPC北京B)

hihoCoder - 1871 Heshen's Account Book (2018ICPC北京B)

時間限制:1000ms

單點時限:1000ms

記憶體限制:512MB

描述

Heshen was an official of the Qing dynasty. He made a fortune which could be comparable to a whole country's wealth by corruption. So he was known as the most corrupt official in Chinese history. But Emperor Qianlong liked, or even loved him so much that was not punished during Qianlong's regime even everybody knew he was totally corrupted.

After Qianlong quit his job, his son Jiaqing took the throne. The new Emperor hated Heshen very much, so he threw Heshen into jail and awarded him death immediately after Qianlong died. Of course Jiaqing would not forget to raid Heshen's house for money --- that was the story of "Heshen fell, Jiaqing full."

Jiaqing's man got a notebook from Heshen's home which was obviously an account book.But the text of the book was written in English! Jiaqing thought all numbers in that account book should stand for money. Please find out all numbers for Jiaqing.

The text of the account book consists only lower case letters, spaces, and digits
('0' - '9'). One or several consecutive digits form a number. But Jiaqing only cared about the ACTUAL numbers among all numbers. Only if a number DOESN'T satisfy any of the conditions below, it is an ACTUAL number:

1) The character to the left of the number is a lower case letter, for example: a123

2) The character to the right of the number is a lower case letter, for example: 123b

3) The number has one or more extra leading zeros, such as 01 , 0012….

Please note that if the last character of a line is a digit, and the first character of the next line is also a digit, those two digits are considered consecutive.

輸入

There are no more than 200 lines. The length of each line is no more than 1000 characters.

And it is guaranteed that every number's length is no more than 18.

There may be spaces at the end of a line, and those spaces matter.

No blank lines in the input. A line consisting of only spaces is not a blank line.

輸出

Print all ACTUAL numbers in a single line in the original order.
Then, count the number of ACTUAL numbers of each line, and print them. A number X only belongs to the line which contains the first digit of X.

樣例解釋

We assume there is no spaces at the end of each line in the Sample Input.

In the sample input, the '3' at the end of the second line and the '2' at the beginning of the third line are considered consecutive, so "1323" is a number.  But this number only belongs to the second line, so the third line has only 2 numbers ---- 14 and 344..

樣例輸入

a19 01 17b
12 bdc 13
23 14 344 bc

樣例輸出

12 1323 14 344
0
2
2

 

解題思路:細節題,行末空格要注意。並且最後一行沒有回車,這個也要注意。這裡有個方法,就是先把所有資料讀入,然後連成一行,同時記錄每一個字元所屬的行即可。然後判斷的時候就截出每一個字串,然後判那個字串是否能成立即可。這裡如果出現了換行,並且前一個字元是字母,就往後面加入一個空格作為分隔符,這樣字元換行問題就解決了。

 

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int MAXN=1000100;
int num[MAXN];
vector<ll> ans;
int pos[MAXN];
string str;

pair<ll,int> check(int l,int r){
    
    for(int i=l;i<=r;i++){
        if(str[i]>='a'&&str[i]<='z')
            return {-1,0};
    }

    ll cur=0;
    if(str[l]=='0'&&l==r){
        return {0,pos[l]};
    }
    else{
        if(str[l]=='0'&&l!=r)
            return {-1,0};
    }

    for(int i=l;i<=r;i++){
        cur=cur*10ll+ll(str[i]-'0');
    }
    return {cur,pos[l]};
}

int main(){
    
    char c;
    int line=0;
    int len=0;
    char last=0;

    while(~scanf("%c",&c)){
        if(c=='\n')
        {
            if(last>='a'&&last<='z'){
                str.push_back(' ');
                pos[len++]=line;
            }
            line++;
        }
        else{

            if(last=='\n'&&c>='a'&&c<='z'){
                str.push_back(' ');
                pos[len++]=line;
            }

            str.push_back(c);
            pos[len++]=line;
        }
        last=c;
    }
    str.push_back(' ');
    pos[len++]=line;


    int l=-1,r=-1;
    for(int i=0;i<len;i++){
        if((str[i]>='a'&&str[i]<='z')||(str[i]>='0'&&str[i]<='9')){
            if(l==-1){
                l=i;
            }
        }
        else{
            if(l!=-1){
                r=i-1;
                pair<ll,int> t=check(l,r);
                if(t.first!=-1){
                    num[t.second]++;
                    ans.push_back(t.first);
                }
                l=-1,r=-1;
            }
        }
    }
    
    for(int i=0;i<ans.size();i++){
        if(ans.size()-1==i)
            cout<<ans[i]<<endl;
        else
            cout<<ans[i]<<" ";
    }

    for(int i=0;i<=line;i++)
        cout<<num[i]<<endl;
    
    return 0;
}