"Algorithmic Learning Notes" 39. String processing Word segmentation SJTU OJ 1302 Indent format

Source: Internet
Author: User

1302. Indent Format description

Little Z wants to play with little y happily, but little y is writing the program. The program is written, but the small y how to debug also can not live. Small Z to help small y see a bit don't want to see, because small y although is cute sister, but her program indentation is not bear to look straight. So little Z decided to help her correct.

Each line of the

program is divided into words and spaces, and a contiguous string of ASCII codes from 33 to 126 is a word, and the words are separated by a space of ASCII 32. The indentation method for small z is specific: For each line of  < Span id= "mathjax-span-1" class= "math" > i   A word whose first character cannot be less than  1   to   i −1   A word, and the position of each word should be as close as possible to the front. For example, a section of code:

__start:__integer;____//_begins_herestop:_integer;_//__ends_here___s:__string;___c:___char;_//_temp_

After the indentation method of small z, it becomes the following:

start:_integer;_//_begins_herestop:__integer;_//_ends___heres:_____string;c:_____char;____//_temp

To make it easier for everyone to read, the underscore here represents a space. Note, of course, that your program actually handles the underline as part of the word.

In order to be able to play early and small y, small Z decided to get a program to help small Y. Of course, this task is yours.

Input Format

The first line of the input data is an integer t(t≤) , which indicates the number of test data. The T test data is then included. Each test data contains several lines, each of which includes at least one word, the length of the word is not more than 80, and the words are separated by a number of spaces. Each row may have prefix spaces and suffix spaces. The length of each row does not exceed 180. The end of a test data is represented by a single line of @ characters.

Output Format

For each test data, you should output rows that are equal to the number of rows for this test data.

Sample Input
1  start:  integer;    // begins herestop: integer; //  ends here   s:  string;   c:   char; // temp @
Sample Output
start: integer; // begins herestop:  integer; // ends   heres:     string;c:     char     // temp




My idea is to think of the entire processed code as a table, the number of rows is the number of rows entered, and the number of columns is the maximum number of words in all rows.
Then the width of each column is set to the length of the longest word in the top word, and then the output can be ~
There is a very handy way to divide a string into several words after getline it is the application of Istringstream.
Because the delimiter is a space, it is very simple and also supports the flow operator
The code below, the core is the word control!
#include <iostream>#include<cstring>#include<vector>#include<sstream>using namespacestd;stringlines[10000];//Store the exact words of each linevector<string> words[10000];//each element is a vector with a list of words in the current lineintCol_len =0;//indicates the maximum number of words in a rowintLine_len =0;intcol_longest[10000];inlineintGetmax (intAintb) {    returna > B?a:b;} voidSplitstring&AMP;STR, vector<string> &ws)    {istringstream iss (str);  Do{        stringWord; ISS>>Word;    Ws.push_back (word); } while(ISS);    Ws.pop_back (); //cout<<ws.size () <<endl;Col_len =Getmax (Col_len,ws.size ());}voidPrint_blank (intN) {     for(inti =0; I < n; ++i) {cout<<" "; }    return;} intMainintargcChar Const*argv[]) {        intN cin>>n;//n Group of data     for(intK =0; K < N; ++k) {Col_len=0, Line_len =0;//Initialize        stringinput; memset (Col_longest,0,sizeof(col_longest)); memset (words,0,sizeof(col_longest));        Getline (Cin,input); //Enter first line         while(input!="@"){            if(input!="") Lines[line_len++] =input;        Getline (Cin,input); }        //split the word and put it into the word list         for(inti =0; i < Line_len; ++i) split (Lines[i],words[i]); //record length of each column = maximum length of words in the forefront          for(inti =0; i < Col_len; ++i) {intMaxLen =0;  for(intj =0; J < Line_len; ++J)if(I <words[j].size ()) {MaxLen=Getmax (Maxlen,words[j][i].size ()); } Col_longest[i]=MaxLen; }         //for (int i = 0; i < Col_len; ++i)// {        //cout<<col_longest[i]<< ""; //}cout<<endl; //Output Results         for(inti =0; i < Line_len; ++i) { for(intj =0; J < Words[i].size (); ++j) {cout<<words[i][j]<<" "; //Padded BlanksPrint_blank (col_longest[j]-words[i][j].size ()); } cout<<Endl; }    }    return 0;} 
View Code

"Algorithmic Learning Notes" 39. String processing Word segmentation SJTU OJ 1302 Indent format

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.