POJ 1750 Java

Source: Internet
Author: User

This problem is very pit dad, a little attention is not PE is RA or tle.

First, test instructions:

The description of the problem is not very clear, mainly through the topic of the example of their own summary, (this is the key, not to find a good law will always tle):

Rule: The following string is compared with the previous string, if the number of the same characters is greater than the number of spaces, then the number of spaces plus 1, if the number of the previous number of the same character is less than the amount of space, then the number of spaces to the same number of characters.

Java implementation of the Code on the web basically did not find, no way, before POJ on a total of 4 predecessors with the Java AC, can only do their own hands,

Start my direct violence compared directly to print, finally the RA solved, but unfortunately tle,

This problem with c,c++ all to 500ms+, with Java or a bit difficult,

I optimized the half-day, racking up the brains, and finally AC:

The following code is shared with lazy people:

import Java.io.bufferedreader;import java.io.ioexception;import java.io.InputStreamReader; Public classMain { Public Staticstringbuffer[]b=Newstringbuffer[ One];  Public Static intBlank;  Public Static voidMain (String[]args) throws ioexception{BufferedReaderinch=NewBufferedReader (NewInputStreamReader (System.inch)); String Str="" ;        String Next; Blank=0;        Init ();  while(Next =inch. ReadLine ())! =NULL) {Write (str,next); STR=Next; }    }     Public Static voidWrite (String str,string next) {intsame =GSN (Str,next); if(same>blank) {Blank++; }Else if(same<blank) {Blank=same; } System. out. println (b[blank]+next); }     Public Static intGSN (String s1,string s2) {//Getsamenumber        intLen = S1.length () >s2.length ()?s2.length (): S1.length (); inti;  for(i=0; i<len;i++){            if(S1.charat (i)! =S2.charat (i)) {                 Break; }        }        returni; }         Public Static voidinit () {StringBuffer sb;  for(intI=0;i< One; i++) {SB=NewStringBuffer ("");  for(intj=0; j<i;j++) {sb.append (" "); } B[i]=SB; }    }}

To add, the print of that space is a waste of time, seemingly no way to print a few spaces in Java, if each time with a for implementation, then definitely tle, because the string length is not greater than 10, so I used the table, this can save a lot of time.

POJ 1750 Java

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.