Port of algorithm problem (201,700-degree internship written test question)

Source: Internet
Author: User

Port

Time limit: c/C + + language 1000MS; other languages 3000MS
Memory limit: 65536KB for C + + language, other languages 589824KB

Title Description:
In order to save the amount of data transmitted by information, a simple compression method is often used to process the port address within a company. Example:
[001011:110011:101101:000000]→[1011:110011:101101:] (compression)
[1011:1101::]→[001011:001101:000000:000000] (decompression)
So, why not write a program to automatically complete compression and decompression?

Input
Entering the first line an integer a,a of 0 indicates that the task is compressed, and a is 1 when the task is decompressed.
Then the second line of a string, indicating the port information of the input program, to ensure that the port information only "0", "1", ":" Three kinds of characters.

Output
Outputs a line of string that represents the port information obtained after compression or decompression.

Sample input
0
001011:110011:101101:000000

Sample output
1011:110011:101101:

Hint
Input Sample 2
1
1011::1101:
Output Sample 2
001011:000000:001101:000000

My answer (time tense, writing complex ...) )

Import Com.sun.deploy.util.stringutils;import Java.util.arrays;import Java.util.scanner;public class Main {public STA        tic void Main (string[] args) {Scanner Scanner = new Scanner (system.in);        int iscomprise = Scanner.nextint ();        String line = Scanner.next ();        /*int iscomprise = 1;        String line = "1011:110011:101101:"; */string result; if (iscomprise = = 0) {//001011:110011:101101:000000//1011:110011:101101:string[] Gro            up = Line.split (":"); for (int j=0; j<group.length; J + +) {if (Group[j].contains ("1")) group[j] = group[j].substring (Group[j].                IndexOf ("1"));            else group[j] = "";        } result = Stringutils.join (Arrays.aslist (group), ":"). Replace ("", ""); } else {//1011:110011:101101://001011:110011:101101:000000 if (Line.charat (line.lengt            H ()-1) = = ': ') line + = ""; string[] group = LINE.SPLit (":");                for (int j=0; j<group.length; J + +) {if (Group[j].equals (")) group[j] =" 0 ";                StringBuilder sb = new StringBuilder ();                for (int i=0; i<6-group[j].length (); i++) Sb.append ("0");            GROUP[J] = Sb.append (Group[j]). toString ();        } result = Stringutils.join (Arrays.aslist (group), ":");    } System.out.println (Result); }}

  

Port of algorithm problem (201,700-degree internship written test question)

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.