0w1

CFR 147 A. Punctuation ( Ad hoc )

Problem - A - Codeforces

題意:
給一行有標點符號,空格,和小寫英文字母的字串。求照英文書寫格式化後的樣子( 字與字之間一個空白,但標點符號和前面的字無空格 )。保證有解。

資料規模:
一行含空白的字串,不超過 1e4 個字元。

解法:
用 stringstream 讀進來之後,依空白分隔拿出來處理。處理的時候,找到一個標點符號後,就立刻分治。

時間 / 空間複雜度:
O( | str | )

需要注意:
またうっかり break するの忘れた...

string buff;

void init(){
    getline( cin, buff );
}

vs word;

void dfs( const string &s ){
    if( s.empty() )
        return;
    int hp = 0;
    for( int i = 0; i < s.size(); ++i )
        if( not ( 'a' <= s[ i ] and s[ i ] <= 'z' ) ){
            hp |= 1;
            dfs( s.substr( 0, i ) );
            word.emplace_back( s.substr( i, 1 ) );
            dfs( s.substr( i + 1 ) );
            break;
        }
    if( not hp )
        word.emplace_back( s );
}

void preprocess(){
    stringstream ss( buff );
    string t;
    while( ss >> t ){
        dfs( t );
    }
}

void solve(){
    for( int i = 0; i < word.size(); ++i ){
        if( 0 == i or ( ( 'a' <= word[ i - 1 ][ 0 ] and word[ i - 1 ][ 0 ] <= 'z' ) and not ( 'a' <= word[ i ][ 0 ] and word[ i ][ 0 ] <= 'z' ) ) )
            ;
        else
            cout << " ";
        cout << word[ i ];
    }
    cout << endl;
}