华为机试:过滤重复单词

华为机试:过滤重复单词_第1张图片


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

int main()
{
	char str[200] = { 0 };
	char strbak[200] = { 0 };
	char *token = NULL;
	map 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;
}


你可能感兴趣的:(经典编程题)