【编程题】字符串压缩算法和常用的substr与replace函数

题目描述:

小Q想要给他的朋友发送一个神秘字符串,但是他发现字符串的过于长了,于是小Q发明了一种压缩算法对字符串中重复的部分进行了压缩,对于字符串中连续的m个相同字符串S将会压缩为[m|S](m为一个整数且1<=m<=100),例如字符串ABCABCABC将会被压缩为[3|ABC],现在小Q的同学收到了小Q发送过来的字符串,你能帮助他进行解压缩么?  


#include <iostream>
#include <string>
using namespace std;
 
int main(){
    string s;
    cin>>s;
    int i = 0;
    while(i < s.length()){//一直比较到s结尾
        if(s[i] == ']'){//如果当前字符为]
            int j = i;//j用来向前寻找与]相匹配的[
            int k = 0;//k用来记录'|'所在位置
            while(s[j] != '['){//前先查找
                if(s[j] == '|')//遇到|,记录下来
                    k = j;
                j--;//退出时候j位于[位置
            }
            int len = stoi(s.substr(j+1,k-j));//长度为j+1到k-j,substr获得字符串s中从第0位开始的长度为5的字符串 
            string s1 = s.substr(k+1,i - k - 1);
            string s2;
            for(int si = 0; si < len; si++){//将识别到的括号内容进行解码
                s2 += s1;
            }
            s = s.replace(j,i-j+1,s2);
            i = j;//替换后i所指向的内容变化,从替换部分的头开始再寻找【重要】 
        }
        i++;
    }
    cout<<s<<endl;
}

字符串之replace()

/*用法一: 
 *用str替换指定字符串从起始位置pos开始长度为len的字符 
 *string& replace (size_t pos, size_t len, const string& str); 
 */  
int main()  
{  
    string line = "this@ is@ a test string!";  
    line = line.replace(line.find("@"), 1, ""); //从第一个@位置替换第一个@为空  
    cout << line << endl;     
    return 0;  
}  
/*用法二: 
 *用str替换 迭代器起始位置 和 结束位置 的字符 
 *string& replace (const_iterator i1, const_iterator i2, const string& str); 
 */  
int main()  
{  
    string line = "this@ is@ a test string!";  
    line = line.replace(line.begin(), line.begin()+6, "");  //用str替换从begin位置开始的6个字符  
    cout << line << endl;     
    return 0;  
}  
/*用法三: 
 *用substr的指定子串(给定起始位置和长度)替换从指定位置上的字符串 
 *string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen); 
 */  
int main()  
{  
    string line = "this@ is@ a test string!";  
    string substr = "12345";  
    line = line.replace(0, 5, substr, substr.find("1"), 3); //用substr的指定子串(从1位置数共3个字符)替换从0到5位置上的line  
    cout << line << endl;     
    return 0;  
}  
/*用法四:string转char*时编译器可能会报出警告,不建议这样做 
 *用str替换从指定位置0开始长度为5的字符串 
 *string& replace(size_t pos, size_t len, const char* s); 
 */  
int main()  
{  
    string line = "this@ is@ a test string!";  
    char* str = "12345";  
    line = line.replace(0, 5, str); //用str替换从指定位置0开始长度为5的字符串  
    cout << line << endl;     
    return 0;  
}  
/*用法五:string转char*时编译器可能会报出警告,不建议这样做 
 *用str替换从指定迭代器位置的字符串 
 *string& replace (const_iterator i1, const_iterator i2, const char* s); 
 */  
int main()  
{  
    string line = "this@ is@ a test string!";  
    char* str = "12345";  
    line = line.replace(line.begin(), line.begin()+9, str); //用str替换从指定迭代器位置的字符串  
    cout << line << endl;     
    return 0;  
}  
/*用法六:string转char*时编译器可能会报出警告,不建议这样做 
 *用s的前n个字符替换从开始位置pos长度为len的字符串 
 *string& replace(size_t pos, size_t len, const char* s, size_t n); 
 */  
int main()  
{  
    string line = "this@ is@ a test string!";  
    char* str = "12345";  
    line = line.replace(0, 9, str, 4);  //用str的前4个字符替换从0位置开始长度为9的字符串  
    cout << line << endl;     
    return 0;  
}  
/*用法七:string转char*时编译器可能会报出警告,不建议这样做 
 *用s的前n个字符替换指定迭代器位置(从i1到i2)的字符串 
 *string& replace (const_iterator i1, const_iterator i2, const char* s, size_t n); 
 */  
int main()  
{  
    string line = "this@ is@ a test string!";  
    char* str = "12345";  
    line = line.replace(line.begin(), line.begin()+9, str, 4);  //用str的前4个字符替换指定迭代器位置的字符串  
    cout << line << endl;     
    return 0;  
}  
/*用法八: 
 *用重复n次的c字符替换从指定位置pos长度为len的内容 
 *string& replace (size_t pos, size_t len, size_t n, char c); 
 */  
int main()  
{  
    string line = "this@ is@ a test string!";  
    char c = '1';  
    line = line.replace(0, 9, 3, c);    //用重复3次的c字符替换从指定位置0长度为9的内容  
    cout << line << endl;     
    return 0;  
}  
/*用法九: 
 *用重复n次的c字符替换从指定迭代器位置(从i1开始到结束)的内容 
 *string& replace (const_iterator i1, const_iterator i2, size_t n, char c); 
 */  
int main()  
{  
    string line = "this@ is@ a test string!";  
    char c = '1';  
    line = line.replace(line.begin(), line.begin()+9, 3, c);    //用重复3次的c字符替换从指定迭代器位置的内容  
    cout << line << endl;     
    return 0;  
}  

 

参考:https://blog.csdn.net/jiary5201314/article/details/52502516

字符串之replace()

#include<string>
#include<iostream>
using namespace std;
int main()
{
  string s("12345asdf");
  string a = s.substr(0,5);     //获得字符串s中从第0位开始的长度为5的字符串
  cout << a << endl;
}
/*
0. 用途:一种构造string的方法

1. 形式:s.substr(pos, n)

2. 解释:返回一个string,包含s中从pos开始的n个字符的拷贝(pos的默认值是0,n的默认值是s.size() - pos,即不加参数会默认拷贝整个s)

3. 补充:若pos的值超过了string的大小,则substr函数会抛出一个out_of_range异常;若pos+n的值超过了string的大小,则substr会调整n的值,只拷贝到string的末尾
*/
————————还可以用vector<char>————————
#include<string>
 
#include<iostream>
 
using namespace std;
 
int main()
 
{
 
	string str1("Hello");
 
	cout << " str1 is:" << str1<<endl;
 
	basic_string<char>str2 = str1.substr(2, 3);
 
	cout << "The substring str1 copied is:" << str2 << endl;
 
	basic_string<char>str3 = str1.substr();
 
	cout << "The default substring str3 is:" << endl;
 
	cout << str3 << endl;
 
	system("pause");
	return 0;
 
}

参考:https://www.cnblogs.com/zhudingtop/p/11394564.html

 

 附:

substr函数的测试代码

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

/*用法一: 
 *用str替换指定字符串从起始位置pos开始长度为len的字符 
 *string& replace (size_t pos, size_t len, const string& str); 
 */  
void test1()  
{  
    string line = "this@ is@ a test string!"; 

    line = line.replace(line.find("@"), 2, "xixixixi"); //从第2个@位置替换第2个@为空  
    cout << line << endl;     
    //return 0;  
}  
/*用法二: 
 *用str替换 迭代器起始位置 和 结束位置 的字符 
 *string& replace (const_iterator i1, const_iterator i2, const string& str); 
 */  
void test2()  
{  
    string line = "this@ is@ a test string!";  
    line = line.replace(line.begin(), line.begin()+7, "1234567");  //用str替换从begin位置开始的7个字符  
    cout << line << endl;     
    //return 0;  
}  
/*用法三: 
 *用substr的指定子串(给定起始位置和长度)替换从指定位置上的字符串 
 *string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen); 
 */  
void test3()  
{  
    string line = "this@ is@ a test string!";  
    string substr = "12345";  
    line = line.replace(1, 3, substr, substr.find("3"), 3); //用substr的指定子串(从1位置数共3个字符)替换从0到5位置上的line  
    cout << line << endl;     
    //return 0;  
}  
/*用法四:string转char*时编译器可能会报出警告,不建议这样做 
 *用str替换从指定位置0开始长度为5的字符串 
 *string& replace(size_t pos, size_t len, const char* s); 
 */  
void test4()  
{  
    string line = "this@ is@ a test string!";  
    char* str = "12345";  
    line = line.replace(0, 5, str); //用str替换从指定位置0开始长度为5的字符串  
    cout << line << endl;     
    //return 0;  
}  
/*用法五:string转char*时编译器可能会报出警告,不建议这样做 
 *用str替换从指定迭代器位置的字符串 
 *string& replace (const_iterator i1, const_iterator i2, const char* s); 
 */  
void test5()  
{  
    string line = "this@ is@ a test string!";  
    char* str = "12345";  
    line = line.replace(line.begin(), line.begin()+9, str); //用str替换从指定迭代器位置的字符串  
    cout << line << endl;     
    //return 0;  
}  
/*用法六:string转char*时编译器可能会报出警告,不建议这样做 
 *用s的前n个字符替换从开始位置pos长度为len的字符串 
 *string& replace(size_t pos, size_t len, const char* s, size_t n); 
 */  
void test6()  
{  
    string line = "this@ is@ a test string!";  
    char* str = "123456789";  
    line = line.replace(0, 9, str, 9);  //用str的前4个字符替换从0位置开始长度为9的字符串  
    cout << line << endl;     
    //return 0;  
}  
/*用法七:string转char*时编译器可能会报出警告,不建议这样做 
 *用s的前n个字符替换指定迭代器位置(从i1到i2)的字符串 
 *string& replace (const_iterator i1, const_iterator i2, const char* s, size_t n); 
 */  
void test7()  
{  
    string line = "this@ is@ a test string!";  
    char* str = "12345";  
    line = line.replace(line.begin(), line.begin()+9, str, 4);  //用str的前4个字符替换指定迭代器位置的字符串  
    cout << line << endl;     
   // return 0;  
}  
/*用法八: 
 *用重复n次的c字符替换从指定位置pos长度为len的内容 
 *string& replace (size_t pos, size_t len, size_t n, char c); 
 */  
void test8()  
{  
    string line = "this@ is@ a test string!";  
    char c = '8';  
    line = line.replace(0, 9, 8, c);    //用重复8次的c字符替换从指定位置0长度为9的内容  
    cout << line << endl;     
   // return 0;  
}  
/*用法九: 
 *用重复n次的c字符替换从指定迭代器位置(从i1开始到结束)的内容 
 *string& replace (const_iterator i1, const_iterator i2, size_t n, char c); 
 */  
void test9()  
{  
    string line = "this@ is@ a test string!";  
    char c = '1';  
    line = line.replace(line.begin(), line.begin()+9, 3, c);    //用重复3次的c字符替换从指定迭代器位置的内容  
    cout << line << endl;     
    //return 0;  
}  
int main(){
	test1();
	test2();
	test3();
	test4();
	test5();
	test6();
	test7();
	test8();
	test9();
	return 0;
}

测试结果:

thisxixixixiis@ a test string!
1234567s@ a test string!
t345@ is@ a test string!
12345 is@ a test string!
12345 a test string!
123456789 a test string!
1234 a test string!
88888888 a test string!
111 a test string!

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章