236A - Boy or Girl In this problem we have to find out if the number of distinct characters in one's user name is odd or even. How to count the unique characters in a string in c++? TIA.
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 151 |
236A - Boy or Girl In this problem we have to find out if the number of distinct characters in one's user name is odd or even. How to count the unique characters in a string in c++? TIA.
Name |
---|
Sort the vector (call this vector v)
Then use v.erase(unique(v.begin(), v.end()), v.end());
Then your answer is just the size of the vector afterwards.
Read about erase and unique to understand what they do.
Alternatively you could insert all the elements in a set and return it's size
If you are interested only in the count of unique characters of a string
x
, it suffices to do the following:can you describe it please?
Check this out. (sorting is necessary to get all similar characters in a continuous segment)
I have understood the sorting. But i can't understood this: unique(x.begin(), x.end()) — x.begin();
Unique return iterator to last character. Unique — begin would give the index of last character which is also the number of unique characters in the string.
Anyway I think using a bucket is better than sorting and unique() since we only have lowercase Latin letters.
One-liner if you're really lazy:
set(s.begin(), s.end()).size()