日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

字符串压缩之C++实现

發布時間:2024/1/1 c/c++ 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 字符串压缩之C++实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目: 輸入一個字符串,輸出對其壓縮過的形式。
? ? 如輸入為:aaabbbccc則輸出為3a3b3c;如輸入為abbc,則輸出為a2bc。
? ? aaabbbccc ?----> ?3a3b3c

? ? abc ? ? ? ?----> ?abc


#include <iostream>
#include <string>
using namespace std;


int main()
{
? ? int cnt = 1;///cnt the number of every char
? ? int pos = 1;
? ? char tmp_A,tmp_B;
? ? string ori_str, sht_str;
? ? string::size_type ori_len;
? ? char c_cnt;
? ? cout<<"input the ori string"<<endl;
? ? cin>>ori_str;


? ? ori_len = ori_str.size();
? ? for (; pos < static_cast<int>(ori_len); ++pos)
? ? {
? ? ? ? tmp_A = ori_str[pos - 1];
? ? ? ? tmp_B = ori_str[pos];

? ? ? ? if (tmp_A != tmp_B && pos != static_cast<int>(ori_len-1))
? ? ? ? ///沒到末尾的時候,AB不同
? ? ? ? {
? ? ? ? ? ? if (cnt == 1)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? sht_str.push_back(tmp_A);
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? c_cnt = cnt + '0';
? ? ? ? ? ? ? ? sht_str.push_back(c_cnt);
? ? ? ? ? ? ? ? sht_str.push_back(tmp_A);
? ? ? ? ? ? ? ? cnt = 1;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? else if (tmp_A != tmp_B && pos == static_cast<int>(ori_len-1))
? ? ? ? ///在末尾,但是A,B不同
? ? ? ? {
? ? ? ? ? ? if (cnt == 1)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? sht_str.push_back(tmp_A);
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? sht_str.push_back(cnt);
? ? ? ? ? ? ? ? sht_str.push_back(tmp_A);
? ? ? ? ? ? ? ? cnt = 1;
? ? ? ? ? ? }
? ? ? ? ? ? sht_str.push_back(tmp_B);
? ? ? ? }
? ? ? ? else if (tmp_A == tmp_B && pos != static_cast<int>(ori_len-1))
? ? ? ? ///不在末尾,AB相同
? ? ? ? {
? ? ? ? ? ? ++cnt;
? ? ? ? }
? ? ? ? else if (tmp_A == tmp_B && pos == static_cast<int>(ori_len-1))
? ? ? ? ///在末尾,AB相同
? ? ? ? {
? ? ? ? ? ? cnt++;
? ? ? ? ? ? c_cnt = cnt + '0';
? ? ? ? ? ? sht_str.push_back(c_cnt);
? ? ? ? ? ? sht_str.push_back(tmp_A);
? ? ? ? }
? ? }


? ? cout<<sht_str;
? ? return 0;
}

總結

以上是生活随笔為你收集整理的字符串压缩之C++实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。