输入一个字符串,输出简单的压缩

1)单字符串压缩 :
输入:ABBBCCD , 输出AB3C2D
2)多字符串压缩
输入:AABCABCD,输出A(ABC)2D
1)压缩单个字符
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
string func(string data){
int pre = data[];
for(int i=;i<(int)data.size();i++){
cout << "find:"<<data[i]<<endl;
char temp = data[i];
if(temp == pre){
int pos = i;
data[pos]='';
cout << "same"<<endl;
temp = data[++i];
while(temp == pre){
data[pos]++;
temp = data[(++i)];
}
i=pos;
data.erase(pos+,(data[pos]-'')-);
}
pre = data[i];
}
return data;
}
int main(){
string result; string s="xxxxxxxyyyyyyz";
result = func(s);
cout << result <<endl;
return ;
}

转一个别人的做法感觉比我的做法好

void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr){
// 安全检查
assert((pInputStr != NULL) && (pOutputStr != NULL));
char mark = *pInputStr;
int count = ;
long j = ; // 压缩之后的str的下标
for (long i = ; i <= lInputLen; i ++){
// 判断是否一样
if (i < lInputLen && mark == pInputStr[i]){
count++;
}
else
{
if (count > ){
//zip str
pOutputStr[j] = count;
pOutputStr[++j] = mark;
j ++;
count = ;
}
else // if (count == 1){
pOutputStr[j] = mark;
j++;
}
mark = pInputStr[i];
}
}
}

2)压缩字符串

第二个没啥思路啊

#include <iostream>#include <vector>#include <algorithm>#include <string>using namespace std;//1 2 3 4 5 1000 2//一个字符串,找最长无重复字母子串.string func(string data){    int pre = data[0];    for(int i=1;i<(int)data.size();i++){        cout << "find:"<<data[i]<<endl;        char temp = data[i];        if(temp == pre){            int pos = i;            data[pos]='2';            cout << "same"<<endl;            temp = data[++i];            while(temp == pre){                data[pos]++;                temp = data[(++i)];            }            i=pos;            data.erase(pos+1,(data[pos]-'0')-2);        }        pre = data[i];    }    return data;}int main(){    string result;
    string s="xxxxxxxyyyyyyz";    result = func(s);    //result.erase(2);    cout << result <<endl;    return 0;}

【练习题】proj2 字符串压缩的更多相关文章

  1. 基于Zlib算法的流压缩、字符串压缩源码

    原文:基于Zlib算法的流压缩.字符串压缩源码 Zlib.net官方源码demo中提供了压缩文件的源码算法.处于项目研发的需要,我需要对内存流进行压缩,由于zlib.net并无相关文字帮助只能自己看源 ...

  2. php字符串压缩

    在PHP中偶尔遇到字符串的压缩,比如一个长字符串,数据库开始设计的字段存不下,但是又不想改数据库字段存储长度,就可以用压缩的方式降低数据字段字符串的长度数量级,把几百个字符的字符串压缩到几十个字符.总 ...

  3. 字符串压缩 stringZip

    1,题目描述 通过键盘输入一串小写字母(a~z)组成的字符串.请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串.压缩规则:1. 仅压缩连续重复出现的字符.比如字符串 ...

  4. ruby直接字符串压缩与解压缩

    ruby2.1.3的核心类中包含了Zlib库,其中的Zlib模块包含了对字符串压缩和解压的方法: irb(main):180:0> Zlib.class => Module irb(mai ...

  5. 记录新项目中遇到的技术及自己忘记的技术点【DES加密解密,MD5加密,字符串压缩、解压,字符串截取等操作】

    一.DES加密.解密 #region DES加密解密 /// <summary> /// 进行DES加密 /// </summary> /// <param name=& ...

  6. C# 使用GZip对字符串压缩和解压

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  7. PAT 1078 字符串压缩与解压(20)(代码+思路)

    1078 字符串压缩与解压(20 分) 文本压缩有很多种方法,这里我们只考虑最简单的一种:把由相同字符组成的一个连续的片段用这个字符和片段中含有这个字符的个数来表示.例如 ccccc 就用 5c 来表 ...

  8. CC05:基本字符串压缩

    题目 利用字符重复出现的次数,编写一个方法,实现基本的字符串压缩功能.比如,字符串"aabcccccaaa"经压缩会变成"a2b1c5a3".若压缩后的字符串没 ...

  9. PAT(B) 1078 字符串压缩与解压(Java)

    题目链接:1078 字符串压缩与解压 (20 point(s)) 题目描述 文本压缩有很多种方法,这里我们只考虑最简单的一种:把由相同字符组成的一个连续的片段用这个字符和片段中含有这个字符的个数来表示 ...

随机推荐

  1. WPF中的Generic.xaml, theme以及custom control

    原文:WPF中的Generic.xaml, theme以及custom control 在Visual Studio中创建自定义控件时,所有控件都将添加到/Themes/Generic.xaml. 最 ...

  2. OpenCV For iOS 1:&#160;连接OpenCV 3.0

    本文的内容參考Instant OpenCV for iOS结合最新的开发平台完毕. 本系列文章採用的的开发环境为: 1)Xcode 6 2)OpenCV for iOS 3.0.0 alpha 接下来 ...

  3. Python 爬虫 —— scrapy

    0. 创建网络爬虫的常规方法 进入命令行(操作系统的命令行,不是 python 的命令行) windows:cmd ⇒ c:\Uses\Adminstrator> Linux:$ 执行:scra ...

  4. 3 WCF一些基础铺垫

    1首先上一张wcf通讯图 a.Proxy代理部分底层调用的是 xxxxClient=> ChannelFactory=>IInpuChannel/IOutChannel... b.Tran ...

  5. EF codefirst第一篇

    一直以来喜欢dbfirst  因为简单,一直不明白为什么codefirst会是主流,根据对ddd的学习终于知道了codefirst的目的 本文是对博客园 小崔的笔记本 文章 EF实体框架之CodeFi ...

  6. C#: Get current keyboard layout\input language

    原文 https://yal.cc/csharp-get-current-keyboard-layout/ On some occasions, you may want to get a " ...

  7. Qt Installer Framework 3.0.1 Released(功能比较强)

    We are happy to announce the release of Qt IFW 3.0.1. 3.0.1 is fully compatible with 2.0.5, which me ...

  8. VC实现程序重启的做法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 很多时候系统有很多配置项,修改了配置项之后能有一个按钮实现系统重启.所谓重启就是杀死系统的当前进程,然后重新开一个新进 ...

  9. 运行时动态伪造vsprintf的va_list

    运行时动态伪造vsprintf的va_list #include <stdio.h> int main() { char* m = (char*) malloc(sizeof(int)*2 ...

  10. wpf border内部元素内边角溢出问题 裁剪效果

    效果图 <Grid> <Border BorderThickness=" BorderBrush="#3c4d72" CornerRadius=&quo ...