1 package trial; 2 // C ++ input: A string consisting of digits and letters, for example, 333aaabb55ppin 3 // output: if a number appears consecutively (two or more) the same character (number or character), delete one. If it is not a number, do not delete it. For example, the output is 33aabb55pin. 4 5 // the core of this sentence is to delete some characters in the string. It feels complicated to process. What characters can be deleted? We observed that a 3, a A, and a P are deleted from this string. What are the rules that meet this requirement? Delete the last 3 In 333, 3AA delete a A, 5pp delete a p, 6 // The rule is that if the first two are numbers, the first character is the same as him, is deleted. 7 Import Java. util. optional; 8 9 public class main6 {10 11 public static void main (string [] ARGs) {12 // todo auto-generated method stub13 using SCN = new using (system. in); 14 While (SCN. hasnext () 15 {16 char c1 [] = SCN. next (). tochararray (); 17 char c2 [] = new char [c1.length]; 18 c2 [0] = c1 [0]; 19 c2 [1] = c1 [1]; 20 // The first two characters cannot be deleted 21 int Index = 2; // The undeleted characters cannot be copied to the new character array, 22 for (INT I = 2; I <c1.length; I ++) 23 {24 if (c1 [I] = c1 [I-1] & (c1 [I-2]> = '0' & c1 [I-2] <= '9 ')) continue; // if conditions are met, delete 25 C2 [index ++] = c1 [I]; // if conditions are not met, copy to C2 array 26 27 28 29 30} 31 32 string ans = ""; 33 for (char C3: C2) 34 {35 ans + = C3; 36} 37 38 system. out. println (ANS); 39} 40 41} 42 43}
2015 trial output: if two or more identical characters (numbers or characters) appear consecutively after a number, delete one character. Do not delete any character after a number. For example, the output is 33aabb55pin.