2012.9华为校招机试
1. 输入一个字符串,去掉重复出现的字符,输出字符串中每个字符只出现一次,如输入"abcdbc122325",输出"abcd1235",给定函数原型为
void deleteString(char * inputString,int length,char *outputString);
2. 输入一个字符串,将连续重复的字符压缩,如输入"xxbaaatwwwwwewwwwwwwwww",输出"2xb3at5we10w", 给定函数原型为
void zipString(char * inputString,int len,char * outputString);
1 #include <iostream> 2 #include <cmath> 3 #include <iomanip> 4 #include <string> 5 #include <cstring> 6 #include <vector> 7 #include <map> 8 #include <set> 9 #include <algorithm> 10 #include <stdio.h> 11 #include <stack> 12 #include <queue> 13 #include<cstdio> 14 using namespace std; 15 16 void deleteString(char * inputString,int length,char *outputString) 17 { 18 const int MaxCharNum=256; 19 bool isUsed[MaxCharNum]; 20 memset(isUsed,0,sizeof(isUsed)); 21 int outputLen = 0; 22 for (int i=0; i<length; i++) 23 { 24 if (!isUsed[inputString[i]]) 25 { 26 isUsed[inputString[i]] = true; 27 outputString[outputLen++] = inputString[i]; 28 } 29 } 30 } 31 32 void zipString(char * inputString,int len,char * outputString) 33 { 34 int outputLen = 0; 35 int i=0; 36 while(i < len) 37 { 38 int j = i; 39 int cnt = 0; 40 while(j<len && inputString[j] == inputString[i]) 41 { 42 j++; 43 cnt++; 44 } 45 if (cnt != 1) 46 { 47 const int MaxDigitalNum = 100; 48 int digitalArray[MaxDigitalNum]; 49 int t = cnt, digitalNum = 0; 50 while(t) 51 { 52 digitalArray[digitalNum++] = t%10; 53 t /= 10; 54 } 55 for (int k=digitalNum-1; k>=0; k--) 56 { 57 outputString[outputLen++] = digitalArray[k]+'0'; 58 } 59 } 60 outputString[outputLen++] = inputString[i]; 61 i = j; 62 } 63 } 64 65 int main() 66 { 67 const int MAX=100; 68 char sourceString1[MAX] = "abcdbc122325"; 69 char objectString1[MAX] = "\0"; 70 deleteString(sourceString1,strlen(sourceString1),objectString1); 71 cout<<objectString1 <<endl; 72 73 char sourceString2[MAX] = "xxbaaatwwwwwewwwwwwwwww"; 74 char objectString2[MAX] = "\0"; 75 zipString(sourceString2,strlen(sourceString2), objectString2); 76 cout<<objectString2 <<endl; 77 return 0; 78 }