字符串压缩之C++实现
来源:互联网 发布:lovelive动作数据资源 编辑:程序博客网 时间:2024/05/16 08:05
如输入为: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++实现
- 字符串压缩之C++实现
- C字符串压缩算法
- 字符串压缩C++实现
- c实现文件压缩
- c#SharpZipLib实现压缩
- 数据结构c语言实现之字符串
- C标准库函数实现之字符串操作
- 蛮力法之字符串匹配(C实现)
- c语言字符串处理-----压缩字符串
- lzw字符串压缩算法实现
- 字符串压缩功能的实现
- 基本字符串压缩Java实现
- c语言实现把‘0’和‘1’字符串转化为二进制压缩保存成二进制文件
- 【c++】Huffman实现文件压缩
- C和指针之字符串之实现strcpy函数
- 字符串压缩——C语言
- 面试5之编写一个方法,实现基本的字符串压缩功能。
- C++primer学习笔记-----4.2算数运算符
- STL中map-set的使用
- Linux中断系列之中断或异常处理(四)
- lintcode 67 二叉树的中序遍历
- 真正的匿名者,TOR 的原理
- 字符串压缩之C++实现
- Craster's Parabolic Projection(CPP)
- 【Web】【JSP】JSTL使用
- poj 2516 Minimum Cost 【图论-网络流-最小费用流-spfa】
- P2P平台借款人与债权人的匹配原理
- NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor 解决办法
- mongodbVUE使用条件
- OpenWRT添加自启脚本
- json和jsonp的区别