华为机试:过滤重复单词

来源:互联网 发布:淘宝账号名怎么改 编辑:程序博客网 时间:2024/05/17 02:47


#include <iostream>#include <math.h>#include <map>#include <vector>#include <string.h>#include <memory>#include <string>#include <stdio.h>using namespace std;int main(){char str[200] = { 0 };char strbak[200] = { 0 };char *token = NULL;map<string, int> wordMap;char* p;string output;int i = 0, j = 0;gets(str);/* 去掉标点符号 */for (i = 0; str[i] != '\0'; i++){if (str[i] == ',' || str[i] == '.'){strbak[j] = ' ';j++;}else{strbak[j] = str[i];j++;}}strbak[j] = '\0';strncpy(str, strbak, strlen(strbak) + 1);/* 分割字符串 */token = strtok(str, " ");while (token != NULL){string  s(token);wordMap[s]++;token = strtok(NULL, " ");}/* 分割字符串 */int count = 0;token = strtok(strbak, " ");while (token != NULL){string s(token);if (wordMap[s] >= 1){if (count == 0){output = output + s;wordMap[s] = 0;}else{output = output + " " + s;wordMap[s] = 0;}count++;}token = strtok(NULL, " ");}cout << output << endl;return 0;}


0 0
原创粉丝点击