Skip to content

Instantly share code, notes, and snippets.

@jhtan
Created July 31, 2014 20:52
Show Gist options
  • Select an option

  • Save jhtan/85871e928366f17fc139 to your computer and use it in GitHub Desktop.

Select an option

Save jhtan/85871e928366f17fc139 to your computer and use it in GitHub Desktop.
SPOJ Desining T-Shirts, ArgCamp14
#include <bits/stdc++.h>
#define MAX 1000000
using namespace std;
struct trie{
map<char, int> sig;
bool termina;
int a, b;
};
trie t[MAX];
int n = 1;
void init() {
for(int i = 0; i < MAX; i++)
t[i].sig.clear(), t[i].termina = false, t[i].a = 0, t[i].b = 0;
}
void insertar(string cad, bool team) {
int pos = 0;
for (int i = 0; i < cad.size(); i++) {
char car = cad[i];
if(t[pos].sig.find(car) == t[pos].sig.end())
t[pos].sig[car] = n++;
pos = t[pos].sig[car];
if(team)t[pos].a++;
else t[pos].b++;
}
t[pos].termina = true;
}
int ans;
void dfs(int pos) {
ans += min(t[pos].a, t[pos].b);
for(map<char, int>::iterator it = t[pos].sig.begin(); it != t[pos].sig.end(); it++)
dfs(it->second);
}
int main() {
int n;
scanf("%d", &n);
while(n != -1) {
init();
string s;
for(int i=0; i<n; i++) {
cin >> s;
insertar(s, false);
}
for(int i=0; i<n; i++) {
cin >> s;
insertar(s, true);
}
ans = 0;
dfs(0);
printf("%d\n", ans);
scanf("%d", &n);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment