【牛客网】---字符集合

【牛客网】 字符集合

如题

输入一个字符串,求出该字符串包含的字符集合
输入描述: 每组数据输入一个字符串,字符串最大长度为100,且只包含字母,不可能为空串,区分大小写。
输出描述: 每组数据一行,按字符串原有的字符顺序,输出字符集合,即重复出现并靠后的字母不输出。
示例1 输入 abcqweracb

输出 abcqwer

思路:

  • 本题利用类似hash(哈希)的方法,因为字符的ASCII码最多是256位,所以只要一个256的数组来存对应字符的ASCII码,然后按字符串字符映射,再遍历输出,初始化为0,出现了为1,输出1次再化为0,这样就避免了重复输出,这样的时间复杂度较低,是一种很优良的算法实现。

代码如下:


#include<string>
#include<iostream>
#include<stdlib.h>
using namespace std;

int main()
{
	string s;
	while (cin >> s)
	{
		int arry[256] = { 0 };
		for (auto ch :   s)
		{
			if (arry[ch] == 0)
			{
				cout << ch;
				arry[ch] = 1;
			}
		}
		cout << endl;
	}
	system("pause");
	return 0;
}
发布了45 篇原创文章 · 获赞 271 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/L19002S/article/details/102938971