Skip to content

Instantly share code, notes, and snippets.

@vslaykovsky-zz
Created January 18, 2015 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vslaykovsky-zz/6c5e404b80806069c1f4 to your computer and use it in GitHub Desktop.
Save vslaykovsky-zz/6c5e404b80806069c1f4 to your computer and use it in GitHub Desktop.
#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