【編程題】字符串壓縮算法和常用的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!

 

 

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