一、題目描述(60分): 通過鍵盤輸入一串小寫字母(a~z)組成的字符串。請編寫一個字符串過濾程序,若字符串中出現多個相同的字符,將非首次出現的字符過濾掉。 比如字符串“abacacde”過濾結果爲“abcde”。 要求實現函數:void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr); 【輸入】 pInputStr: 輸入字符串 lInputLen: 輸入字符串長度 【輸出】 pOutputStr: 輸出字符串,空間已經開闢好,與輸入字符串等長; 【注意】只需要完成該函數功能算法,中間不需要有任何IO的輸入輸出 示例 輸入:“deefd” 輸出:“def” 輸入:“afafafaf” 輸出:“af” 輸入:“pppppppp” 輸出:“p” main函數已經隱藏,這裏保留給用戶的測試入口,在這裏測試你的實現函數,可以調用printf打印輸出 當前你可以使用其他方法測試,只要保證最終程序能正確執行即可,該函數實現可以任意修改,但是不要改變函數原型。一定要保證編譯運行不受影響。 二、題目描述(40分): 通過鍵盤輸入一串小寫字母(a~z)組成的字符串。請編寫一個字符串壓縮程序,將字符串中連續出席的重複字母進行壓縮,並輸出壓縮後的字符串。 壓縮規則: 1、僅壓縮連續重複出現的字符。比如字符串"abcbc"由於無連續重複字符,壓縮後的字符串還是"abcbc"。 2、壓縮字段的格式爲"字符重複的次數+字符"。例如:字符串"xxxyyyyyyz"壓縮後就成爲"3x6yz"。 要求實現函數: void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr); 【輸入】 pInputStr: 輸入字符串 lInputLen: 輸入字符串長度 【輸出】 pOutputStr: 輸出字符串,空間已經開闢好,與輸入字符串等長; 【注意】只需要完成該函數功能算法,中間不需要有任何IO的輸入輸出 示例 輸入:“cccddecc” 輸出:“3c2de2c” 輸入:“adef” 輸出:“adef” 輸入:“pppppppp” 輸出:“8p
#include<iostream>
#include<set> using namespace std; void stringFilter(const char *pInputStr, long InputLen, char *pOutputStr) { set<char>TestChar; pair<set<char>::iterator,bool>mark; int j = 0; for(int i = 0; i < InputLen;i++) { mark = TestChar.insert(pInputStr[i]); if (mark.second ==true) { pOutputStr[j++]=pInputStr[i]; } } pOutputStr[j] = '\0'; } void stringFilter2(const char *pInputStr, long InputLen, char *pOutputStr) { bool mark[26] = {0};int j = 0; for(int i = 0;i < InputLen; i++) { if (0 == mark[pInputStr[i] - 'a']) { mark[pInputStr[i] - 'a'] = true; pOutputStr[j++] = pInputStr[i]; } } pOutputStr[j]='\0'; } void stringZip(const char *pInputStr, long InputLen, char *pOutputStr) { int beg = 0,end = 0,j = 0,pos = 0; while (pInputStr[pos] != '\0') { beg = pos; end = pos+1; for (int i = pos; i < InputLen; i++) { if (pInputStr[end] == pInputStr[i]) { end++; }else { pos = end; break; } } if (end - beg == 1) { pOutputStr[j++] = pInputStr[beg]; }else { pOutputStr[j++]=(end-beg)+'0'; pOutputStr[j++]=pInputStr[beg]; } } pOutputStr[j]='\0'; // if (end - beg == 1) // { // pOutputStr[j++] = pInputStr[beg]; // }else // { // pOutputStr[j++]=end-beg+1-'0'; // pOutputStr[j++]=pInputStr[beg]; // } } int main() { char *p1 = "xxxyyyzz"; char *p2 = new char[9]; stringZip(p1,8,p2); while(*p2 !='\0') { cout <<*p2; p2++; } return 0; }