Huawei machine question 2, Huawei machine question
Question 2: Question description (40 points): enter a string of lower-case letters (~ Z. Compile a character string compression program to compress duplicate letters that are present continuously in the string and output the compressed character string. Compression rules: 1. Only the characters that appear repeatedly are compressed. For example, because the string "abcbc" does not have consecutive repeated characters, the compressed string is still "abcbc". 2. The format of the compressed field is "number of repeated characters + character ". For example, after the string "xxxyyyyz" is compressed, it becomes "3x6yz". required implementation function: void stringZip (const char * pInputStr, long lInputLen, char * pOutputStr); [input] pInputStr: input string lInputLen: length of the input string [Output] pOutputStr: Output string, with good space and length of the input string. [note] You only need to complete this function algorithm, there is no need for input/output sample input with any IO in the middle: "cccddecc" output: "3c2de2c" input: "adef" output: "adef" input: "pppppppp" output: "8 p" my program:
# Include <iostream> # include <string> using namespace std; int main () {string s; cin> s; for (int I = 0; I <s. size (); I ++) {int t = 1; for (int j = I + 1; j <s. size (); j ++) {if (s [j] = s [I]) + + t;} if (t> 1) {s. erase (I + 1, t-1); char p [10]; // VC6.0 cannot use to_string; itoa (t, p, 10); s. insert (I, p) ;}}cout <s <endl; return 0 ;}