欢迎来到天天文库
浏览记录
ID:39580672
大小:46.50 KB
页数:4页
时间:2019-07-06
《华为2014笔试算法题(字符串处理部分)》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、华为2014笔试算法题(字符串处理部分)1.通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。比如字符串“abacacde”过滤结果为“abcde”。要求实现函数:voidstringFilter(constchar*pInputStr,longlInputLen,char*pOutputStr);【输入】pInputStr: 输入字符串 lInputLen: 输入字符串长度 【输出】pOutputS
2、tr:输出字符串,空间已经开辟好,与输入字符串等长; 【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出示例 输入:“deefd” 输出:“def”输入:“afafafaf” 输出:“af”输入:“pppppppp” 输出:“p”main函数已经隐藏,这里保留给用户的测试入口,在这里测试你的实现函数,可以调用printf打印输出当前你可以使用其他方法测试,只要保证最终程序能正确执行即可,该函数实现可以任意修改,但是不要改变函数原型。一定要保证编译运行不受影响1.#includ
3、e 2.#include 3. 4.using namespace std; 5. 6.bool g_flag[26]; 7.void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr) 8.{ 9. assert(pInputStr != NULL); 10. int i = 0; 11. if (pInputStr
4、== NULL
5、
6、 lInputLen <= 1) 12. { 13. return; 14. } 1. const char *p = pInputStr; 2. while(*p != ' ') 3. { 4. if (g_flag[(*p - 'a')]) 5. { 6. p++; 7. }else{ 8. pOutputStr[i++] = *p; 9.
7、 g_flag[*p - 'a'] = 1; 10. p++; 11. } 12. } 13. pOutputStr[i] = ' '; 14.} 15.int main() 16.{ 17. memset(g_flag,0,sizeof(g_flag)); 18. char input[] = "abacacde"; 19. char *output = new char[strlen(in
8、put) + 1]; 20. stringFilter(input,strlen(input),output); 21. cout<9、,压缩后的字符串还是"abcbc"。2、压缩字段的格式为"字符重复的次数+字符"。例如:字符串"xxxyyyyyyz"压缩后就成为"3x6yz"。要求实现函数: voidstringZip(constchar*pInputStr,longlInputLen,char*pOutputStr);【输入】pInputStr: 输入字符串 lInputLen: 输入字符串长度【输出】pOutputStr:输出字符串,空间已经开辟好,与输入字符串等长;【注意】只需要完成该函数功能算法,中间不需要有任何I10、O的输入输出示例 输入:“cccddecc” 输出:“3c2de2c”输入:“adef” 输出:“adef”输入:“pppppppp”输出:“8p”1.#include 2.#include 3.using namespace std; 4. void stringZip(const char *pInp
9、,压缩后的字符串还是"abcbc"。2、压缩字段的格式为"字符重复的次数+字符"。例如:字符串"xxxyyyyyyz"压缩后就成为"3x6yz"。要求实现函数: voidstringZip(constchar*pInputStr,longlInputLen,char*pOutputStr);【输入】pInputStr: 输入字符串 lInputLen: 输入字符串长度【输出】pOutputStr:输出字符串,空间已经开辟好,与输入字符串等长;【注意】只需要完成该函数功能算法,中间不需要有任何I
10、O的输入输出示例 输入:“cccddecc” 输出:“3c2de2c”输入:“adef” 输出:“adef”输入:“pppppppp”输出:“8p”1.#include 2.#include 3.using namespace std; 4. void stringZip(const char *pInp
此文档下载收益归作者所有