Created
January 18, 2015 17:51
-
-
Save vslaykovsky-zz/6c5e404b80806069c1f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <set> | |
using namespace std; | |
int common_prefix(const string& a, const string& b) { | |
int i = 0; | |
while (i < a.size() && i < b.size() && a[i] == b[i]) | |
++i; | |
return i; | |
} | |
int main(int ac, char* av[]) { | |
int t; | |
cin >> t; | |
for (int i = 0; i < t; ++i) { | |
int n; | |
cin >> n; | |
set<string> words; | |
int total_chars = 0; | |
for (int k = 0; k < n; ++k) { | |
string word; | |
cin >> word; | |
words.insert(word); | |
set<string>::iterator it = words.find(word); | |
int max_pref = 0; | |
if (it != words.begin()) { | |
auto prev = it; | |
--prev; | |
max_pref = max(max_pref, common_prefix(*prev, word)); | |
} | |
++it; | |
if (it != words.end()) | |
max_pref = max(max_pref, common_prefix(*it, word)); | |
if (max_pref != word.size()) | |
++max_pref; | |
total_chars += max_pref; | |
} | |
cout << "Case #" << (i + 1) << ": " << total_chars << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment