Importjava.util.stack;//dehong Hongju oldjiang DP Public classRedchrysanthemum {Private StaticString result = ""; Private Staticstack<string> stack =NewStack<>(); Public Static voidMain (string[] args) {//" 4[b]3[ac]" is "BBBBACACAC" after decompression//" 2[d3[c]" "DCCCDCCC" after decompressionString str = "EF10[D3[CC]]"; System.out.print (Process (str)); } Private Staticstring Process (String str) {string in, out; intLength =str.length (); for(inti = 0; i < length; i++) { in=string.valueof (Str.charat (i)); //is not equal to the right parenthesis in the stack if(!in.equals ("]") ) {Stack.push (in); } Else{String tmpstr= ""; String Tar= ""; out=Stack.pop (); //the stacks are all [alphanumeric, get the numbers together, pop out one, and then go to the following loop. //If you have more than one letter, you will continue to walk this loop, the numbers go below. while(!out.equals ("[") ) {Tmpstr= out +Tmpstr; //pop out one, continue to judge, is the letter on the popout =Stack.pop (); } //number 2[d3[c]]String numstr = ""; while(Stack.peek (). CharAt (0) >= ' 0 ' && stack.peek (). CharAt (0) <= ' 9 ') { //access to the top of the stack, is the number on the stitching, and then jumped one. Numstr = Stack.peek () +Numstr; Stack.pop (); if(Stack.isempty ()) { Break; } } //get the number left in the left bracket intnum =Integer.parseint (NUMSTR); for(intj = 0; J < num; J + +) {tar= Tar +Tmpstr; } stack.push (TAR); } } while(Stack.size () > 0) {String finalstr=Stack.pop (); Result= Finalstr +result; } returnresult; }}
Java code to extract strings in the specified format