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)