Task: | Anagrams |
Sender: | sspilsbury |
Submission time: | 2020-09-26 14:00:34 +0300 |
Language: | C++ (C++11) |
Status: | READY |
Result: | WRONG ANSWER |
test | verdict | time | |
---|---|---|---|
#1 | WRONG ANSWER | 0.12 s | details |
#2 | WRONG ANSWER | 0.57 s | details |
#3 | WRONG ANSWER | 0.19 s | details |
#4 | WRONG ANSWER | 0.61 s | details |
#5 | ACCEPTED | 0.60 s | details |
#6 | ACCEPTED | 0.60 s | details |
#7 | ACCEPTED | 0.01 s | details |
#8 | ACCEPTED | 0.01 s | details |
#9 | ACCEPTED | 0.01 s | details |
Code
#include <string> #include <vector> #include <iostream> #include <unordered_map> #include <algorithm> typedef std::pair<int, int> pair; struct pair_hash { template <class T1, class T2> std::size_t operator() (const std::pair<T1, T2> &pair) const { return std::hash<T1>()(pair.first) ^ std::hash<T2>()(pair.second); } }; template <typename T> void print_array(std::vector<T> const &v) { for (auto x: v) { std::cout << x << " "; } std::cout << std::endl; } int main(void) { int n; std::cin >> n; std::vector<std::string> words(n); for (int i = 0; i < n; ++i) { std::cin >> words[i]; } // We can make a hashtable of words, first compute // the hash of every word as just the sum of its characters // where there is a match between two words we can compare // with an is_anagram function or something like that std::vector<long> hashes(n); std::vector<size_t> lengths(n); for (int i = 0; i < n; ++i) { auto const &w = words[i]; for (char c : w) { hashes[i] += (int) (c); } lengths[i] = w.size(); } // Now that we have the hashes, we can put things into groups // where their hashes are equal and they are actually anagrams // of each other. This only happens if the words are the same size, I think. std::unordered_map<pair, std::vector<int>, pair_hash> ht; for (int i = 0; i < n; ++i) { auto key = pair(hashes[i], lengths[i]); ht[key].push_back(i); } for (auto it = ht.begin(); it != ht.end();) { if (it->second.size() < 2) { it = ht.erase(it); } else { ++it; } } std::cout << ht.size() << std::endl; for (auto it = ht.begin(); it != ht.end(); ++it) { auto const &idxs = it->second; std::cout << it->second.size() << std::endl; for (auto const &i: idxs) { std::cout << words[i] << std::endl; } } return 0; }
Test details
Test 1
Verdict: WRONG ANSWER
input |
---|
68760 aakkonen aakkosellinen aakkosellisesti aakkosellisuus ... |
correct output |
---|
3076 2 haaraantua raahaantua 2 ... |
user output |
---|
2003 2 yksityiskeskustelu yksityiskohtaisuus 2 ... Truncated |
Test 2
Verdict: WRONG ANSWER
input |
---|
370099 a aa aaa aah ... |
correct output |
---|
30178 2 basiparachromatin marsipobranchiata 2 ... |
user output |
---|
2262 2 wy xx 2 ... Truncated |
Test 3
Verdict: WRONG ANSWER
input |
---|
100000 cnhmuewgnum dxkmhzhetnmxadtcy hfjqwavsiguwpludsketibe xwxolrmvkz ... |
correct output |
---|
0 |
user output |
---|
4664 2 zwy zvz 2 ... Truncated |
Test 4
Verdict: WRONG ANSWER
input |
---|
400000 vlcsa eltwde wdcwwkubs tmuxbirj ... |
correct output |
---|
0 |
user output |
---|
1390 2 yzy zzx 3 ... Truncated |
Test 5
Verdict: ACCEPTED
input |
---|
400000 ebhfigdacjlk aecfdijlhkgb jfekhbidacgl cehajbidfklg ... |
correct output |
---|
1 400000 abcdeighjlfk abcdeiglhfjk abcdfkilejgh ... |
user output |
---|
1 400000 ebhfigdacjlk aecfdijlhkgb jfekhbidacgl ... Truncated |
Test 6
Verdict: ACCEPTED
input |
---|
400000 cbaabghadefb hbbgfaeabdac abaedcbgfbha hcfadbbbeaag ... |
correct output |
---|
1 400000 aaabbbcfegdh aaabbbcfghed aaabbbdcgfhe ... |
user output |
---|
1 400000 cbaabghadefb hbbgfaeabdac abaedcbgfbha ... Truncated |
Test 7
Verdict: ACCEPTED
input |
---|
1 a |
correct output |
---|
0 |
user output |
---|
0 |
Test 8
Verdict: ACCEPTED
input |
---|
2 ab ba |
correct output |
---|
1 2 ab ba |
user output |
---|
1 2 ab ba |
Test 9
Verdict: ACCEPTED
input |
---|
2 aa ab |
correct output |
---|
0 |
user output |
---|
0 |