String compression for questions on Huawei Machine

Source: Internet
Author: User
Enter a string of lowercase letters (~ Z.                      Compile a character string compression program to compress duplicate letters that are present continuously in the string,                      And output the compressed string.                       Compression rules: 1. Only the characters that appear repeatedly are compressed. For example, string "abcbc" The compressed string is still "abcbc" .                2. The format of the compressed field is "Repeated characters + characters" . Example: String "xxxyyyyyyz" After compression, it becomes "3x6yz"
# Include <iostream> # include <string. h> using namespace STD; int main () {cout <... "<Endl; char STR [100] =" "; char outstr [100] =" "; cin. getline (STR, 100); int Len = strlen (STR); int I = 0, j = 0; int CNT = 1; char c = STR [0]; for (I = 1; I <Len; I ++) {If (STR [I] = c) {CNT ++;} else {If (CNT = 1) {outstr [J ++] = C;} else {outstr [J ++] = CNT + '0'; outstr [J ++] = C ;} C = STR [I]; CNT = 1 ;}} if (C! = STR [J-1]) {If (CNT = 1) {outstr [J ++] = C;} else {outstr [J ++] = CNT + '0 '; outstr [J ++] = C ;}} cout <outstr <Endl; return 0 ;}


String compression for questions on Huawei Machine

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: 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.