1.5字符串-基本字符串压缩

题目描述

利用字符重复出现的次数,编写一个方法,实现基本的字符串压缩功能。比如,字符串“aabcccccaaa”经压缩会变成“a2b1c5a3”。若压缩后的字符串没有变短,则返回原先的字符串。

给定一个string iniString为待压缩的串(长度小于等于3000),保证串内字符均由大小写英文字母组成,返回一个string,为所求的压缩后或未变化的串。

测试样例
"aabcccccaaa"
返回:"a2b1c5a3"
 
"welcometonowcoder"
返回:"welcometonowcoder"

解决方法1:常规思路,用数组进行赋值,但是涉及到数字到字符的转化比较麻烦,还涉及到下标的变话,我的方法不完整,只统计了最多40个重复的压缩。
#include <iostream>
#include<string>
using namespace std;
string zipString(string);
string zipString(string iniString) {
        int len = iniString.length();
        char outString[10000];
        int t = 0;
        int d = 0;
        for(int i = 0; i < len; i++) {
			    outString[t] = iniString[i];
				outString[++t] = 1 + 48;
				if(iniString[i] != iniString[i+1]) {
					t++;
				}
				
                else {
                	d = 1;   //标志是否可以压缩 
                int j = i; 
                int count = 0;
                while(iniString[i] == iniString[j] && j < len) {
                    count++;
                    j++;
                }
                i = j-1;  //由于这里i循环会加1,所以这里赋值减1 
                if(count < 10) {
                	char out = count + 48;
			     	outString[t] = out ; 
			     	t++;
                }
                else if(count < 20) {
                	char out1 = 1 + 48;
					char out2 = count - 10 + 48; 
					outString[t] = out1;
					outString[t+1] = out2;
					t = t + 2;
                }
                else if(count < 30) {
                	char out1 = 2 + 48;
					char out2 = count - 20 + 48;
					outString[t] = out1;
					outString[t+1] = out2;
					t = t + 2;
                }
                else if(count < 40) {
                	char out1 = 3 + 48;
					char out2 = count - 30 + 48;
					outString[t] = out1;
					outString[t+1] = out2;
					t = t + 2;
                }
                
            }
			
			
        }
        if(d) return outString;
		else return iniString;
    }
int main(int argc, char** argv) {
    string a = "jjjjjjxxxxxxxooZLFFFFF";
    cout << zipString(a);
    return 0;
     
}

解决方法2:a)直接用字符串解决,使用append函数进行添加字符串;b)stringstream进行整型数据到字符串的转换。完美解决上面出现的两个问题。程序思路,先统计个                   数,再进行字符串的链接,注意最后一个字符串的链接,已经跳出了循环。

ng namespace std;

string zipString(string);
string intToString(int);

string intToString(int v) {
	stringstream t;
	string temp;
	t << v;
	temp = t.str();
	return temp;
}
string zipString(string iniString) {
        int len = iniString.length();
        string newStr;
        char newChar = iniString[0];
        int count = 1;
        int d = 0;
        for(int i = 1; i < len; i++) {
        	if(newChar == iniString[i]) {
        		count++;
				d = 1;
			}
			else {
			    
				newStr += newChar;
				newStr.append(intToString(count));
				newChar = iniString[i];
				count = 1;
			}
		}
		newStr += newChar;
		newStr.append(intToString(count));
		if(d) return newStr;
		else  return iniString;
    }

int main(int argc, char** argv) {
    string a = "jjjjjjxxxxxxxooZLFFFFF";
    string b = "abcdefg";
    cout << zipString(a) << endl;
    cout << zipString(b) << endl;
    return 0;
     
}

  

原文地址:https://www.cnblogs.com/xiaohaigege/p/5175480.html