Problem:
Problem C
i18n
文字数が多い英単語はしばしば，前後の1文字ずつだけを残し，それ以外の中間部分をその文字数で置き換える形で省略されることがある．
たとえば，"internationalization" は "i18n"，"localization" は "l10n"，"accessibility" は "a11y" などと略記される．
ここではこの略記を一般化し，前後の文字を
1
文字以上であれば好きなだけ残してよいものとし，この表記を「字数略記」と呼ぶこととする．
例えば，"i18n"，"int13tion"，"i1ternationalization" はいずれも "internationalization" の字数略記であるが，"i123n"，"i4national3tion"，"19n" はいずれも "internationalization" の字数略記ではない．ただし，どの区間も字数に置き換えないそのままの "internationalization" も，"internationalization" の字数略記として有効であるものとする．
あなたは，英小文字のみからなる単語が
N
個含まれた辞書を持っており，この辞書内の単語をそれぞれ字数略記に変換した辞書を作ろうと思っている．
ここで，もしもある字数略記が変換前の辞書に含まれる複数の単語の字数略記として有効であると，その字数略記だけを見て元の単語を一意に復元することができなくなってしまう．よって，そのような字数略記を変換後の辞書に含めることはできない．
例えば，"love" と "live" がともに辞書に含まれる場合，いずれかでも "l2e" という字数略記に変換してしまうと，"l2e" を見たときに元が "love" だったのか "live" だったのかわからず，復元できなくなってしまう．この場合，例えば "love" を "lo1e"，"live" を "li1e" と字数略記すれば，いずれも元の単語を一意に特定することができる．
与えられた辞書に対し，字数略記変換後の辞書として考えられるもののうち，変換に使用した整数の和として考えられるものの最大値を求めよ．
ただし，単語そのままを字数略記とする場合に使用した整数は，0 であるとみなす．
Input
入力は 50 個以下のデータセットからなる．各データセットは次の形式で表される．
N
s
1
s
2
...
s
N
最初の行は辞書に含まれる単語の個数
N
からなる (
1 ≤ N ≤1,000
)．続く
N
行には，変換前の辞書に含まれる単語が，各行ごとに
1
つ与えられる．各単語の長さは
3
以上
30
以下であり，英小文字アルファベットのみからなる．辞書に含まれる単語はすべて相異なることが保証される．
入力の終わりは，
1
つのゼロからなる行で表される．
Output
各データセットについて，各単語を一意に復元できるよう字数略記に変換するとき，使用した整数の和の最大値を
1
行に出力せよ．
Sample Input
5
abcxxfg
abcyyfg
abcdefx
azcdefg
www
3
internationalization
localization
accessibility
4
love
live
cat
cut
3
class
comes
cross
2
aaa
aaaaaaaaaa
0
Output for the Sample Input
16
39
2
6
9

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;

void solve(int n){
	vector<string>S(n);
	rep(i,n)cin>>S[i];
	typedef pair<string,pair<int,string>>PS;
	map<PS,int>M;
	vector<vector<PS>>V(n);
	rep(i,n){
		for(int l=0;l<S[i].size();l++){
			for(int r=S[i].size()-1;l+1<r;r--){
				string L,R;
				for(int j=0;j<=l;j++)L.push_back(S[i][j]);
				for(int j=r;j<S[i].size();j++)R.push_back(S[i][j]);
				V[i].push_back(PS(L,pair<int,string>(r-l-1,R)));
				M[PS(L,pair<int,string>(r-l-1,R))]++;
			}
		}
		V[i].push_back(PS(S[i],pair<int,string>(0,"")));
		M[PS(S[i],pair<int,string>(0,""))]++;
	}
	int ans=0;
	rep(i,n){
		int cd=0;
		for(auto e:V[i]){
			if(M[e]==1)cd=max(cd,e.second.first);
		}
		ans+=cd;
	}
	cout<<ans<<endl;
}

int main(){
	int n;
	while(1){
		cin>>n;
		if(!n)return 0;
		solve(n);
	}
}
accuracy: 1
time_ms: 810
memory_kb: 102728
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define x second
#define y first

void sol(int N) {
	string s;
	vector<vector<string>> A(31);

	for (int i = 0 ; i < N ; i ++ )
	{
		cin >> s;
		int len = s.size();
		A[len].emplace_back() = s;
	}

	int ans = 0 ;

	for (int len = 3 ; len <= 30 ; len++) {
		using sv = string_view;
		struct Hash {
			size_t operator()(const pair<sv, sv>& p) const {
				return hash<sv>()(p.first) ^ (hash<sv>()(p.second) << 1);
			}
		};

		unordered_map<pair<sv, sv>, int, Hash> freq;
		for (const auto& t : A[len]) {
			sv ts = t;
			for (int i = len - 2; i >= 0; i--) {
				for (int j = 1; i + j < len; j++) {
					sv a = ts.substr(0, j);
					sv b = ts.substr(i + j);
					freq[{a, b}]++;
				}
			}
		}
		for (const auto& t : A[len]) {
			sv ts = t;
			int start_i = len - 2;
			bool found = false;
			for (int d = 0; d < len; d++) {
				for (int sign : {1, -1}) {
					int i = start_i + sign * d;
					if (i < 0 || i > len - 2) continue;

					for (int j = 1; i + j < len; j++) {
						sv a = ts.substr(0, j);
						sv b = ts.substr(i + j);
						if (freq[{a, b}] <= 1) {
							ans += i;
							start_i = i;
							found = true;
							break;
						}
					}
					if (found) break;
				}
				if (found) break;
			}
		}
	}

	cout << ans << '\n';
}

int main()
{
#ifdef AJAVA_DEBUG
freopen("output.txt", "wt", stdout);
#endif
    cin.tie(nullptr)->sync_with_stdio(false);
	int N;
	for (int i = 1 ; cin >> N && N ; i ++ ) sol(N);
    return 0;
}
accuracy: 1
time_ms: 260
memory_kb: 33900
final_rank: 3

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
using ll = long long;
#define rep(i,n) for(ll i=0; i<ll(n); i++)

struct Node {
    ll w;
    ll nx[26];
    Node(){
        rep(i,26) nx[i] = -1;
        w = 0;
    }
};

bool testcase(){
    ll N; cin >> N;
    if(N == 0) return false;
    vector<vector<string>> S(31);
    rep(i,N){ string s; cin >> s; S[s.size()].push_back(s); }
    ll ans = 0;
    for(auto& F : S) if(!F.empty()){
        ll len = F[0].size();
        //cout << "len = " << len << endl;
        //for(auto& s : F) cout << s << " ";
        //cout << endl;
        vector<Node> trie(len-2);
        auto acc = [&](ll p, ll c) -> ll {
            if(trie[p].nx[c] == -1){
                ll n = trie.size();
                trie.push_back(Node());
                trie[p].nx[c] = n;
            }
            //cout << "p = " << p << " , c = " << c << " , n = " << trie[p].nx[c] << endl;
            return trie[p].nx[c];
        };
        rep(d,len-2){
            for(auto& s : F){
                string buf = s.substr(len-d-1) + s.substr(0,len-d-1);
                ll p = d;
                trie[p].w += 1;
                for(char c : buf){
                    p = acc(p, c-'a');
                    trie[p].w += 1;
                }
            }
        }
        for(auto& s : F){
            ll minl = len;
            rep(d,len-2){
                string buf = s.substr(len-d-1) + s.substr(0,len-d-1);
                ll p = d;
                ll t = 0;
                for(char c : buf){
                    p = acc(p, c-'a'); t++;
                    if(trie[p].w == 1){
                        minl = min(minl, max(t, d+2));
                        break;
                    }
                }
            }
            ans += len - minl;
        }
    }
    cout << ans << "\n";
    return true;
}

int main(){
    while(testcase());
    return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 246600
final_rank: 19

code_snippet: #ifdef LOCAL
#define _GLIBCXX_DEBUG
#pragma GCC optimize("O0")
#else
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif

#include <bits/stdc++.h>
// #include <bits/extc++.h>
using namespace std;

// #include <atcoder/modint>
// using mint = atcoder::modint998244353;
// istream& operator>>(istream& in, mint& x) {
//     long long a;
//     in >> a;
//     x = a;
//     return in;
// }
// ostream& operator<<(ostream& out, const mint& x) {
//     return out << x.val();
// }

using ll = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;

template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'000'000'000;
template <>
constexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;
template <>
constexpr double infty<double> = infty<ll>;
template <>
constexpr long double infty<long double> = infty<ll>;

using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
using vvi = vvc<int>;
using vvl = vvc<ll>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pqg = std::priority_queue<T, vector<T>, greater<T>>;
template <class T, class U>
using umap = unordered_map<T, U>;

// template <typename K>
// using tree = __gnu_pbds::tree<K, __gnu_pbds::null_type, std::less<>,
//                               __gnu_pbds::rb_tree_tag,
//                               __gnu_pbds::tree_order_statistics_node_update>;

#define vv(type, name, h, ...) \
    vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...)     \
    vector<vector<vector<type>>> name( \
        h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)         \
    vector<vector<vector<vector<type>>>> name( \
        a, vector<vector<vector<type>>>(       \
               b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))

// FOR(a) :=  for (ll _ = 0; _ < (ll)a; ++_)
// FOR(i, a) := for (ll i = 0; i < (ll)a; ++i)
// FOR(i, a, b) := for (ll i = a; i < (ll)b; ++i)
// FOR(i, a, b, c) := for (ll i = a; i < (ll)b; i += (c))
// FOR_R(a) := for (ll i = (a) - 1; i >= 0; --i)
// FOR_R(i, a) := for (ll i = (a) - 1; i >= 0; --i)
// FOR_R(i, a, b) := for (ll i = (b) - 1; i >= (ll)a; --i)
#define FOR1(a) for (ll _ = 0; _ < (ll)a; ++_)
#define FOR2(i, a) for (ll i = 0; i < (ll)a; ++i)
#define FOR3(i, a, b) for (ll i = a; i < (ll)b; ++i)
#define FOR4(i, a, b, c) for (ll i = a; i < (ll)b; i += (c))
#define FOR1_R(a) for (ll i = (a) - 1; i >= 0; --i)
#define FOR2_R(i, a) for (ll i = (a) - 1; i >= 0; --i)
#define FOR3_R(i, a, b) for (ll i = (b) - 1; i >= (ll)a; --i)
#define overload4(a, b, c, d, e, ...) e
#define overload3(a, b, c, d, ...) d
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FOR_R(...) overload3(__VA_ARGS__, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__)

#define FOR_subset(t, s) \
    for (int t = (s); t >= 0; t = (t == 0 ? -1 : (t - 1) & (s)))
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }
int popcnt_mod_2(int x) { return __builtin_parity(x); }
int popcnt_mod_2(u32 x) { return __builtin_parity(x); }
int popcnt_mod_2(ll x) { return __builtin_parityll(x); }
int popcnt_mod_2(u64 x) { return __builtin_parityll(x); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

template <typename T>
T floor(T a, T b) {
    return a / b - (a % b && (a ^ b) < 0);
}
template <typename T>
T ceil(T x, T y) {
    return floor(x + y - 1, y);
}
template <typename T>
T bmod(T x, T y) {
    return x - y * floor(x, y);
}
template <typename T>
pair<T, T> divmod(T x, T y) {
    T q = floor(x, y);
    return {q, x - q * y};
}

template <typename T, typename U>
T POW(U x_, int n) {
    T x = x_;
    T ret = 1;
    while (n > 0) {
        if (n & 1) ret *= x;
        x *= x;
        n >>= 1;
    }
    return ret;
}

template <typename T, typename U>
T SUM(const vector<U> &A) {
    T sm = 0;
    for (auto &&a : A) sm += a;
    return sm;
}

#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) \
    sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()

template <class T, class S>
inline bool chmax(T &a, const S &b) {
    return (a < b ? a = b, 1 : 0);
}
template <class T, class S>
inline bool chmin(T &a, const S &b) {
    return (a > b ? a = b, 1 : 0);
}

// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
    vc<int> A(S.size());
    FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
    return A;
}

template <typename T, typename U>
vector<T> cumsum(vector<U> &A, int off = 1) {
    int N = A.size();
    vector<T> B(N + 1);
    FOR(i, N) { B[i + 1] = B[i] + A[i]; }
    if (off == 0) B.erase(B.begin());
    return B;
}

template <typename T>
vector<int> argsort(const vector<T> &A) {
    vector<int> ids(A.size());
    iota(all(ids), 0);
    sort(all(ids),
         [&](int i, int j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
    return ids;
}

// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vc<int> &I) {
    vc<T> B(I.size());
    FOR(i, I.size()) B[i] = A[I[i]];
    return B;
}

template<class... T>
constexpr auto min(T... a){
    return min(initializer_list<common_type_t<T...>>{a...});
}
template<class... T>
constexpr auto max(T... a){
    return max(initializer_list<common_type_t<T...>>{a...});
}

void print(){
    cout << '\n';
}
template<class T>
void print(const T& a){
    cout << a << '\n';
}
template<class T, class... Ts>
void print(const T& a, const Ts&... b){
    cout << a;
    (cout << ... << (cout << ' ', b));
    cout << '\n';
}
template<class T>
void print(vector<T> &a){
    for (int i = 0; i < (int)a.size(); ++i) {
        cout << a[i] << " \n"[i == (int)a.size() - 1];
    }
}
template<class T>
void print(vector<T> &&a){
    for (int i = 0; i < (int)a.size(); ++i) {
        cout << a[i] << " \n"[i == (int)a.size() - 1];
    }
}
template<class T>
void print(vector<vector<T>> &a){
    for (int i = 0; i < (int)a.size(); ++i) {
        for (int j = 0; j < (int)a[i].size(); ++j) {
            cout << a[i][j] << " \n"[j == (int)a[i].size() - 1];
        }
    }
}
template<class T>
void print(vector<vector<T>> &&a){
    for (int i = 0; i < (int)a.size(); ++i) {
        for (int j = 0; j < (int)a[i].size(); ++j) {
            cout << a[i][j] << " \n"[j == (int)a[i].size() - 1];
        }
    }
}
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }

#ifdef LOCAL
// https://zenn.dev/sassan/articles/19db660e4da0a4
#include "/Library/cpp-dump/dump.hpp"
#define dump(...) cpp_dump(__VA_ARGS__)
// CPP_DUMP_DEFINE_EXPORT_OBJECT(mint, val());
#else
#define dump(...)
#define CPP_DUMP_SET_OPTION(...)
#define CPP_DUMP_DEFINE_EXPORT_OBJECT(...)
#define CPP_DUMP_DEFINE_EXPORT_ENUM(...)
#define CPP_DUMP_DEFINE_DANGEROUS_EXPORT_OBJECT(...)
#endif


//----------------------------------------------------------------

bool solve() {
    ll N;
    cin >> N;
    if (N == 0) {
        return false;
    }
    vvc<string> sss(40);
    FOR(N) {
        string s;
        cin >> s;
        sss[s.size()].emplace_back(s);
    }
    ll ans = 0;

    FOR(lll, 40) {
        vc<string> &ss = sss[lll];
        ll l = ss.size();
        FOR(i, l) {
            string &s = ss[i];
            FOR_R(ctb, 1, s.size() - 1) {
                bool flg = false;
                FOR(a, 1, (ll)s.size() - ctb) {
                    ll b = (ll)s.size() - ctb - a;
                    string prf = s.substr(0, a);
                    string sfx = s.substr((ll)s.size() - b);
                    bool ok = true;
                    FOR(j, l) {
                        if (i == j) continue;
                        string prf2 = ss[j].substr(0, a);
                        string sfx2 = ss[j].substr((ll)s.size() - b);
                        if (prf == prf2 && sfx == sfx2) {
                            ok = false;
                            break;
                        }
                    }
                    if (ok) {
                        dump(s, a, b, ctb);
                        ans += ctb;
                        flg = true;
                        break;
                    }
                }
                if (flg) break;
            }
        }
    }
    print(ans);

    return true;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(20);
    CPP_DUMP_SET_OPTION(max_line_width, 80);
    CPP_DUMP_SET_OPTION(log_label_func, cpp_dump::log_label::filename());
    CPP_DUMP_SET_OPTION(enable_asterisk, true);
    while (solve()) {
    }
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 3584
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(a) (a).begin(), (a).end()
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; 0 <= i; --i)
template <typename T> bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
void solve();
int N;
string s[1<<10];
signed main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  cout << fixed << setprecision(20);
  int TESTCASES = 1<<30;
  //cin >> TESTCASES;
  while (TESTCASES--) {
    cin>>N;
    if(N==0)break;
    rep(i,N)cin>>s[i];
    solve();
  }
}
string calc(int i,int l,int k){
  string ret=s[i].substr(0,l);
  ret+=to_string(k);
  int r=(int)s[i].size()-k-l;
  ret+=s[i].substr((int)s[i].size()-r,r);
  return ret;
}
multiset<string>st;
void solve() {
  st.clear();
  rep(i,N){
    int siz=s[i].size();
    for(int k=siz-2;1<=k;--k){
      for(int l=1;l+k+1<=siz;++l){
        st.insert(calc(i,l,k));
      }
    }
  }
  int ans=0;
  rep(i,N){
    int siz=s[i].size();
    bool flag=0;
    for(int k=siz-2;1<=k&&!flag;--k){
      for(int l=1;l+k+1<=siz;++l){
        string res=calc(i,l,k);
        auto it=st.lower_bound(res);
        ++it;
        if(it==st.end()||*it!=res){
          ans+=k;
          flag=1;
          break;
        }
      }
    }
  }
  cout<<ans<<'\n';
}
accuracy: 1
time_ms: 370
memory_kb: 51456
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template <class T>
using MaxHeap = std::priority_queue<T>;
template <class T>
using MinHeap = std::priority_queue<T, vector<T>, greater<T>>;
#define rep2(i, n) for (ll i = 0; i < (n); i++)
#define rep3(i, l, r) for (ll i = (l); i < (r); i++)
#define rrep2(i, n) for (ll i = n; i-- > 0;)
#define rrep3(i, r, l) for (ll i = (r); i-- > (l);)
#define overload(a, b, c, d, ...) d
#define rep(...) overload(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define rrep(...) overload(__VA_ARGS__, rrep3, rrep2)(__VA_ARGS__)
#define all(x) begin(x), end(x)
bool chmin(auto& lhs, auto rhs) {
    return lhs > rhs ? lhs = rhs, 1 : 0;
}
bool chmax(auto& lhs, auto rhs) {
    return lhs < rhs ? lhs = rhs, 1 : 0;
}
struct IOIO {
    IOIO() {
        std::cin.tie(0)->sync_with_stdio(0);
    }
} ioio;

void solve() {
    while (true) {
        //  一単語あたりにあり得る略記は
        //  高々900個

        //  複数あるものに関しては使ってはダメ
        //  そうでないなら使ってよい
        int n;
        cin >> n;
        if (n == 0) break;
        vector<pair<string, int>> S;
        rep(i, n) {
            string s;
            cin >> s;
            int m = ssize(s);
            rep(l, 1, m - 1) rep(r, l + 1, m) {
                string t;
                int len = 0;
                rep(i, m) {
                    if (i < l) {
                        t.push_back(s[i]);
                    } else if (l <= i && i < r) {
                        len++;
                    } else {
                        if (len != 0) t += to_string(len);
                        len = 0;
                        t.push_back(s[i]);
                    }
                }
                S.emplace_back(t, i);
            }
        }
        sort(all(S));
        vector<int> ans(n);
        for (int l = 0, r = 0; l < ssize(S); l = r) {
            while (r < ssize(S) && S[l].first == S[r].first) r++;
            if (l + 1 == r) {
                auto [s, i] = S[l];
                string num;
                for (auto c: s)
                    if (isdigit(c)) num.push_back(c);
                if (num.empty()) num = "0";
                chmax(ans[i], stoll(num));
            }
        }
        cout << reduce(all(ans)) << '\n';
    }
}

int main() {
    int t = 1;
    //  cin >> t;
    while (t--) solve();
}
accuracy: 1
time_ms: 290
memory_kb: 51780
final_rank: 4

code_snippet: #line 1 "naive.cpp"
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <cassert>
using namespace std;
using ll = long long;
template<class T> inline bool chmax(T& a, const T& b) {if (a<b) {a=b; return true;} return false;}
template<class T> inline bool chmin(T& a, const T& b) {if (b<a) {a=b; return true;} return false;}
constexpr int INTINF = 1000001000;
constexpr int INTMAX = 2147483647;
constexpr ll LLMAX = 9223372036854775807;
constexpr ll LLINF = 4450000000011100000;

#line 1 "/home/harui/CompetitiveProgramming/library/debug.hpp"



#ifdef LOCAL
#define dbg(x) std::cerr << __LINE__ << " : " << #x << " = " << (x) << std::endl
#else
#define dbg(x) true
#endif

#line 12 "/home/harui/CompetitiveProgramming/library/debug.hpp"
#include <utility>
#include <set>


template <typename T, typename U>
std::ostream& operator<<(std::ostream& os, const std::pair<T, U>& p) {
  os << '(' << p.first << ", " << p.second << ')';
  return os;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
  os << '[';
  for (int i=0; i<int(vec.size()); i++) {
    os << vec[i];
    if (i != int(vec.size())-1) os << ", ";
  }
  os << ']';
  return os;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::set<T>& st) {
  os << '{';
  auto itr = st.begin();
  while (itr != st.end()) {
    os << *itr;
    itr = next(itr);
    if (itr != st.end()) os << ", ";
  }
  os << '}';
  return os;
}



#line 16 "naive.cpp"

struct Info {
  string prefix,  suffix;
  int len;

  Info(string p, string s, int l): prefix(p), suffix(s), len(l) {}

  bool operator<(const Info& rhs) const { 
    if (len != rhs.len) return len < rhs.len;
    if (prefix != rhs.prefix) return prefix < rhs.prefix;
    return suffix < rhs.suffix;
  }
};

void solve(int N) {
  vector<string>SS(N); for (int i=0; i<N; i++) cin >> SS[i];

  map<Info, int> presuf_map;

  for (int i=0; i<N; i++) {
    int M = int(SS[i].size());
    // len: prefixとsuffixで残す文字数の合計。
    // 2 <= len <= M
    // M+1-2 = M-1通りある
    for (int len=2; len<=M; len++) {
      // 1 <= l < len-1
      // len-1通り
      for (int l=1; l+1<=len; l++) {
        int r = len-l;
        assert(l >= 1 && r >= 1 && l+r == len);
        // pref: s[0, l),
        string suff, pref;
        for (int j=0; j<l; j++) pref += SS[i][j];
        // suff: s[M-r, M)
        for (int j=0; j<r; j++) suff += SS[i][M-r+j];

        presuf_map[Info(pref,suff, M)]++;
      }
    }
  }

  int ans =0;
  for (int i=0; i<N; i++) {
    int M = int(SS[i].size());
    // len: prefixとsuffixで残す文字数の合計。
    // 2 <= len <= M
    // M+1-2 = M-1通りある
    int presuf_sum = M;
    // presuf_sumは最小化したい
    // lenは小さい順にみているのでまあ一番最初に見つけたやつがpresuf_sum最小
    for (int len=2; len<=M && presuf_sum >= M; len++) {
      // 1 <= l < len-1
      // len-1通り
      for (int l=1; l+1<=len && presuf_sum >= M; l++) {
        int r = len-l;
        assert(l >= 1 && r >= 1 && l+r == len);

        // pref: s[0, l),
        string suff, pref;
        for (int j=0; j<l; j++) pref += SS[i][j];
        // suff: s[M-r, M)
        for (int j=0; j<r; j++) suff += SS[i][M-r+j];

        //dbg(SS[i]);
        //dbg(len);
        //dbg(l);
        //dbg(pref);
        //dbg(presuf_sum);
        //dbg(r);
        //dbg(suff);

        if (presuf_map[Info(pref,suff, M)] == 1) {
          chmin(presuf_sum, len);
          dbg("exists!");
          dbg(pref);
          dbg(presuf_sum);
          dbg(suff);
          break;
        }
      }
    }

    assert(presuf_sum <= M);
    ans += M - presuf_sum;
    dbg(SS[i]);
    dbg(presuf_sum);
    dbg(M-presuf_sum);
  }
  cout << ans << endl;
}

int main() {
  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  while (true) {
    int N; cin >> N;
    if (N == 0) break;
    solve(N);
  }
  return 0;
}
accuracy: 1
time_ms: 450
memory_kb: 65152
final_rank: 10

code_snippet: #include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
#define int long long

void solve(int n) {
    vector<string> dict(n);
    for (int i=0; i<n; ++i) {
        cin >> dict[i];
    }
    sort(dict.begin(), dict.end());
    // for (auto x: dict) {
    //     cout << x << endl;
    // }

    //[length][pref]
    vector<map<string, int>> len_pre(31), len_suf(31);
    vector<map<pair<string, string>, int>> len_pair(31);
    for (int i=0; i<n; ++i) {
        string s = dict[i];
        int len = s.size();
        string t = s;
        reverse(t.begin(), t.end());
        for (int j=1; j<=len; ++j) {
            len_pre[len][s.substr(0, j)]++;
            len_suf[len][t.substr(0, j)]++;
        }
        for (int j=1; j<=len; ++j) {
            for (int k=1; k<=len; ++k) {
                len_pair[len][{s.substr(0, j), t.substr(0, k)}]++;
            }
        }
    }

    int sum = 0;
    for (int i=0; i<n; ++i) {
        string s = dict[i];
        int len = s.size();
        string t = s;
        reverse(t.begin(), t.end());

        int mx = -1;
        for (int j=1; j<=len; ++j) {
            for (int k=1; k<=len; ++k) {
                if (len_pair[len][{s.substr(0, j), t.substr(0, k)}] == 1) {
                    mx = max(mx, len-j-k);
                }
            }
        }
        sum += mx;
    }
    cout << sum << endl;
}

signed main() {
    while(true) {
        int k;
        cin >> k;
        if (k == 0) break;
        solve(k);
    }
}

/*
memo

abcdefx
abcxxfg
abcyyfg
azcdefg
www

a12345x
a123xfg
a123yfg
az1234g
w1w


class
comes
cross

cl12s
c12es
cr12s


*/
accuracy: 1
time_ms: 1030
memory_kb: 142336
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using a2 = array<ll, 2>;
using a3 = array<ll, 3>;

template <typename A> void chmin(A &l, const A &r) {
    if(r < l)
        l = r;
}
template <typename A> void chmax(A &l, const A &r) {
    if(l < r)
        l = r;
}

ll mod = 998244353;

vector<char> isprime;
void init() {
    isprime.assign(1000005, 1);
    isprime[0] = 0;
    isprime[1] = 0;
    for(int i = 2; i < 1000005; i++) {
        if(isprime[i]) {
            for(int j = i * 2; j < 1000005; j += i) {
                isprime[j] = 0;
            }
        }
    }
}

class UnionFind {
  private:
    int sz;
    vector<int> par, size_;

  public:
    UnionFind() {}
    UnionFind(int node_size) : sz(node_size), par(sz), size_(sz, 1) {
        iota(par.begin(), par.end(), 0);
    }
    int find(int x) {
        if(par[x] == x)
            return x;
        else
            return par[x] = find(par[x]);
    }
    void unite(int x, int y) {
        x = find(x), y = find(y);
        if(x == y)
            return;
        if(size_[x] < size_[y])
            swap(x, y);
        par[y] = x;
        size_[x] += size_[y];
    }
    int size(int x) {
        x = find(x);
        return size_[x];
    }
    bool same(int x, int y) { return find(x) == find(y); }
};

ll n;
vector<string> s;
void input() {
    cin >> n;
    s.resize(n);
    for(auto &x : s)
        cin >> x;
}

void solve() {
    map<tuple<string, string,int>, int> mp;
    for(auto &x : s) {
        for(int i = 1; i < x.size() - 1; i++) {
            for(int j = 1; i + j < x.size(); j++) {
                tuple<string, string,int> p;
                get<0>(p) = x.substr(0, i);
                get<1>(p) = x.substr(i + j, x.size() - (i + j));
                get<2>(p) = j;
                mp[p]++;
            }
        }
    }

    ll ans = 0;
    for(auto &x : s) {
        ll mx = 0;
        for(int i = 1; i < x.size() - 1; i++) {
            for(int j = 1; i + j < x.size(); j++) {
                tuple<string, string,int> p;
                get<0>(p) = x.substr(0, i);
                get<1>(p) = x.substr(i + j, x.size() - (i + j));
                get<2>(p) = j;
                if(mp[p] == 1) {
                    chmax(mx, (ll)j);
                }
            }
        }
        ans += mx;
    }
    cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    // init();
    while(1) {
        input();
        if(n == 0)
            break;
        solve();
    }

    return 0;
}
accuracy: 1
time_ms: 840
memory_kb: 60292
final_rank: 13

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define N 200005
#define F0r(i,n) for(int i=0;i<n;i++)
#define R0f(i,n) for(int i=n-1;i>=0;i--)
#define For(i,a,b) for(int i=a;i<=b;i++)
#define Ror(i,a,b) for(int i=b;i>=a;i--)

struct node{
    int nxt[26],cnt,p;
};

void solve(){
    int n;
    cin>>n;
    if(!n)exit(0);
    vector<vector<string>> vs(31);
    
    F0r(i,n){
        string s;
        cin>>s;
        vs[s.size()].push_back(s);
    }
    
    int ans=0;
    
    For(i,3,30){
        
        int n=vs[i].size();
        
        if(!n)continue;
        
        vector<node> v(i-2);
        
        auto add = [&](string s,int st,int i) -> void {
            int now=st;
            for(auto &x:s){
                int id;
                if(x>='a'&&x<='z'){
                    id=x-'a';
                    if(!v[now].nxt[id])now=v[now].nxt[id]=v.size(),v.push_back(node());
                    else now=v[now].nxt[id];
                }
                v[now].cnt++;
            }
        };
        
        F0r(id,n){
            string &s=vs[i][id];
            int n=s.size();
            For(l,1,i-2)add(s.substr(n-l)+s.substr(0,n-l),l-1,id);
        }
        
        
        F0r(id,n){
            string &s=vs[i][id];
            int val=0;
            For(l,1,i-2){
                int n=s.size();
                string t=s.substr(n-l)+s.substr(0,n-l);
                int now=l-1,res=i;
                for(auto &c:t){
                    now=v[now].nxt[c-'a'];
                    res--;
                    if(v[now].cnt==1){
                        val=max(val,min(res,i-l-1));
                    }
                }
            }
            ans+=val;
        }
        
        
    }
    
    cout<<ans<<'\n';
}


int main(){
    ios_base::sync_with_stdio(false);cin.tie(nullptr);
    int t=1;
    // cin>>t;
    while(true)solve();
    return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 129480
final_rank: 15

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
using ll = long long;
#define rep(i,n) for(ll i=0; i<ll(n); i++)

struct Node {
    ll w;
    ll nx[26];
    Node(){
        rep(i,26) nx[i] = -1;
        w = 0;
    }
};

bool testcase(){
    ll N; cin >> N;
    if(N == 0) return false;
    vector<vector<string>> S(31);
    rep(i,N){ string s; cin >> s; S[s.size()].push_back(s); }
    ll ans = 0;
    for(auto& F : S) if(!F.empty()){
        ll len = F[0].size();
        //cout << "len = " << len << endl;
        //for(auto& s : F) cout << s << " ";
        //cout << endl;
        vector<Node> trie(len-2);
        auto acc = [&](ll p, ll c) -> ll {
            if(trie[p].nx[c] == -1){
                ll n = trie.size();
                trie.push_back(Node());
                trie[p].nx[c] = n;
            }
            //cout << "p = " << p << " , c = " << c << " , n = " << trie[p].nx[c] << endl;
            return trie[p].nx[c];
        };
        rep(d,len-2){
            for(auto& s : F){
                string buf = s.substr(len-d-1) + s.substr(0,len-d-1);
                ll p = d;
                trie[p].w += 1;
                for(char c : buf){
                    p = acc(p, c-'a');
                    trie[p].w += 1;
                }
            }
        }
        for(auto& s : F){
            ll minl = len;
            rep(d,len-2){
                string buf = s.substr(len-d-1) + s.substr(0,len-d-1);
                ll p = d;
                ll t = 0;
                for(char c : buf){
                    p = acc(p, c-'a'); t++;
                    if(trie[p].w == 1){
                        minl = min(minl, max(t, d+2));
                        break;
                    }
                }
            }
            ans += len - minl;
        }
    }
    cout << ans << "\n";
    return true;
}

int main(){
    while(testcase());
    return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 245696
final_rank: 20

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;
constexpr int iINF = 1'000'000'000;

bool solve () {
    int N;
    cin >> N;
    if (N == 0) {
        return false;
    }
    vector<vector<string>> s(40);
    for (int i = 0; i < N; i++) {
        string t;
        cin >> t;
        s[t.size()].push_back(t);
    }

    int ans = 0;
    for (int len = 0; len < 40; len++) {
        // くり抜く長さ
        auto& A = s[len];
        for (int i = 0; i < A.size(); i++) {
            int rem = 0;
            for (int l = 1; l < len - 1; l++) {
                for (int r = l; r < len - 1; r++) {
                    bool ok = true;
                    for (int j = 0; j < A.size(); j++) {
                        if (i == j) {
                            continue;
                        }

                        // 一意になっていますか？？
                        // 前
                        bool same1 = true;
                        for (int k = 0; k < l; k++) {
                            if (A[i][k] != A[j][k]) {
                                same1 = false;
                                break;
                            }
                        }

                        // うしろ
                        bool same2 = true;
                        for (int k = len - 1; r < k; k--) {
                            if (A[i][k] != A[j][k]) {
                                same2 = false;
                                break;
                            }
                        }
                        if (same1 && same2) {
                            ok = false;
                        }
                    }

                    if (ok) {
                        rem = max(rem, r - l + 1);
                    }
                }
            }

            ans += rem;
        }
    }

    cout << ans << "\n";
    return true;
}

int main () {
    while (solve()) {}
}
accuracy: 1
time_ms: 3140
memory_kb: 3460
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define ALL(v) (v).begin(), (v).end()
using ll = long long;

int main() {
    while (true) {
        int N; cin >> N;
        if (N == 0) break;
        vector<string> S(N);
        vector<int> len;
        for (int i = 0; i < N; i++) {
            cin >> S[i];
            len.emplace_back(S[i].size());
        }
        sort(ALL(len));
        len.erase(unique(ALL(len)), len.end());
        int K = len.size();
        vector<int> inv(len.back() + 1, 0);
        for (int i = 0; i < K; i++) inv[len[i]] = i;
        vector<vector<string>> g(K);
        for (int i = 0; i < N; i++) {
            g[inv[S[i].size()]].emplace_back(S[i]);
        }
        int ans = 0;
        for (int i = 0; i < K; i++) {
            int M = g[i].size();
            map<pair<string, string>, int> mp;
            for (int j = 0; j < M; j++) {
                vector<string> pre, suf;
                for (int k = 0; k < len[i]; k++) {
                    pre.emplace_back(g[i][j].substr(0, k+1));
                    suf.emplace_back(g[i][j].substr(k, len[i] - k));
                }
                for (auto x : pre) {
                    for (auto y : suf) {
                        if ((int)x.size() + (int)y.size() > len[i]) continue;
                        mp[make_pair(x, y)]++;
                    }
                }
            }
            for (int j = 0; j < M; j++) {
                int add = 0;
                for (int l = 0; l < len[i]; l++) {
                    for (int r = l+1; r < len[i]; r++) {
                        auto p = g[i][j].substr(0, l+1);
                        auto s = g[i][j].substr(r, len[i] - r);
                        if (mp[make_pair(p, s)] == 1) {
                            add = max(add, r - l - 1);
                        }
                    }
                }
                ans += add;
            }
        }
        cout << ans << "\n";
    }
}
accuracy: 1
time_ms: 560
memory_kb: 58496
final_rank: 11

code_snippet: ﻿#line 1 "domestic_japan/2022_jag/c.cpp"
#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(),v.end()
#define resort(v) sort(v.rbegin(),v.rend())
using ll = long long;
using ull = unsigned long long;
using vll=vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll,ll>;
using vp=vector<pair<ll, ll>>;
using djks=priority_queue<P, vp, greater<P>>;

const ll inf=1ll<<60;
#define mod10 (ll)1e9+7
#define mod99 (ll)998244353
const double PI = acos(-1);

#define rep(i,n) for (ll i=0;i<(n);++i)
#define per(i,n) for(ll i=(n)-1;i>=0;--i)
#define rep2(i,a,n) for (ll i=(a);i<(n);++i)
#define per2(i,a,n) for (ll i=(n)-1;i>=(a);--i)


template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }

ll dx[] = {1, 0, -1, 0, -1, 1, -1, 1};
ll dy[] = {0, 1, 0, -1, -1, 1, 1, -1};

void solve(ll n) {
    vector<string> v(n);
    rep(i,n)cin>>v[i];
    ll ans = 0;
    rep(i,n) {
        ll mn = 0;
        rep2(a, 1, v[i].size()-1){
            // 前からa文字残すとき, 後から何文字消す必要があるか？
            ll mx = 0;
            rep(j,n){
                if(i==j || v[i].size() != v[j].size()) continue;
                // prefixが一致するか
                bool is_same_prefix = true;
                rep(k, min(a, ll(v[j].size()))) {
                    is_same_prefix = is_same_prefix && v[i][k] == v[j][k];
                }

                if(is_same_prefix){
                    bool checked = false;
                    rep(k, min(v[i].size(), v[j].size())) {
                        if(v[i][v[i].size()-k-1] != v[j][v[j].size()-k-1]) {
                            chmax(mx,k);
                            checked = true;
                            break;
                        }
                    }
                    if(!checked) {
                        mx = min(v[i].size(), v[j].size());
                        break;
                    }
                }
            }
            chmax(mn, max(ll(v[i].size())-(a+mx)-1,0LL));
        }
        ans += mn;
    }

    cout << ans << '\n';
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int k;cin>>k;
    while(k>0){
        solve(k);
        cin>>k;
    }
}
accuracy: 1
time_ms: 450
memory_kb: 3540
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)
#define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__)
#define OVERLOAD_REP(_1, _2, _3, _4, name, ...) name
#define REP1(n) for(ll i=0;i<n;i++)
#define REP2(i, n) for (ll i=0;i<n;i++)
#define REP3(i, a, n) for (ll i=a;i<n;i++)
#define REP4(i, a, b, n) for(ll i=a;i<n;i+=b)
#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)
#define OVERLOAD_RREP(_1, _2, _3, _4, name, ...) name
#define RREP1(n) for(ll i=n;i>=0;i--)
#define RREP2(i, n) for(ll i=n;i>=0;i--)
#define RREP3(i, a, n) for(ll i=n;i>=a;i--)
#define RREP4(i, a, b, n) for(ll i=n;i>=a;i-=b)
#define rrep(...) OVERLOAD_RREP(__VA_ARGS__, RREP4, RREP3, RREP2, RREP1)(__VA_ARGS__)
#define foa(a,v)  (auto& a : (v))
#define uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))
#define len(n) (long long)(n).size()
#define pb push_back
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vs = vector<string>;
using vvs = vector<vs>;
using vvvs = vector<vvs>;
using vld = vector<ld>;
using vvld = vector<vld>;
using vvvld = vector<vvld>;
using vc = vector<char>;
using vvc = vector<vc>;
using vvvc = vector<vvc>;
using pll = pair<ll,ll>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;
template<class... T>
constexpr auto min(T... a){
    return min(initializer_list<common_type_t<T...>>{a...});
}
template<class... T>
constexpr auto max(T... a){
    return max(initializer_list<common_type_t<T...>>{a...});
}
template<class... T>
void input(T&... a){
    (cin >> ... >> a);
}
ll modpow(ll a,ll b,ll c){
    ll ans = 1;
    while (b){
        if (b & 1){
            ans *= a;
            ans %= c;
        }
        a *= a;
        a %= c;
        b /= 2;
    }
    return ans;
}
#define INT(...) int __VA_ARGS__; input(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)
#define ULL(...) ull __VA_ARGS__; input(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)
#define STR(...) string __VA_ARGS__; input(__VA_ARGS__)
#define CHA(...) char __VA_ARGS__; input(__VA_ARGS__)
#define VLL(name,length) vll name(length);rep(i,length){cin >> name[i];}
#define VVLL(name,h,w) vvll name(h,vll(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVLL(name,a,b,c) vvvll name(a,vvll(b,vll(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define VI(name,length) vi name(length);rep(i,length){cin >> name[i];}
#define VVI(name,h,w) vvi name(h,vi(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVI(name,a,b,c) vvvi name(a,vvll(b,vi(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define VLD(name,length) vld name(length);rep(i,length){cin >> name[i];}
#define VVLD(name,h,w) vvld name(h,vld(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVLD(name,a,b,c) vvvld name(a,vvld(b,vld(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define VC(name,length) vc name(length);rep(i,length){cin >> name[i];}
#define VVC(name,h,w) vvc name(h,vc(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVC(name,a,b,c) vvvc name(a,vvc(b,vc(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define VS(name,length) vs name(length);rep(i,length){cin >> name[i];}
#define VVS(name,h,w) vvs name(h,vs(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVS(name,a,b,c) vvvs name(a,vvs(b,vs(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define PLL(name) pll name;cin>>name.first>>name.second;
#define VPLL(name,length) vpll name(length);rep(i,length){cin>>name[i].first>>name[i].second;}

void print(){cout << "\n";}
template<class T, class... Ts>
void print(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\n';}
void print(vll x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void print(vvll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}


void solve(ll n,vs a){
    map<string,ll> mp;
    rep(i,n){
        string s = a[i];
        mp[s]++;
        rep(j,1,len(s)){
            rep(k,j+1,len(s)){
                string t;
                rep(l,j){
                    t.push_back(s[l]);
                }
                t.append(to_string(k-j));
                rep(l,k,len(s)){
                    t.push_back(s[l]);
                }
                mp[t]++;
                //print(s,t);
            }
            
        }
    }
    ll ans = 0;

    rep(i,n){
        string s = a[i];
        ll temp = 0;
        mp[s]++;
        rep(j,1,len(s)){
            rep(k,j+1,len(s)){
                string t;
                rep(l,j){
                    t.push_back(s[l]);
                }
                t.append(to_string(k-j));
                rep(l,k,len(s)){
                    t.push_back(s[l]);
                }
                if(mp[t] == 1){
                    temp = max(temp,k-j);
                }
            }
            
        }
        ans += temp;
    }
    print(ans);
}

int main(){
    while(1){
        LL(n);
        if(n == 0){break;}
        VS(a,n);
        solve(n,a);
    }
}
accuracy: 1
time_ms: 540
memory_kb: 48384
final_rank: 6

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<int(n);i++)
#define sz(a) int(a.size())

bool solve(){
	int N;cin>>N;
	if(N==0)return false;

	vector<string>S(N);
	rep(i,N) cin>>S[i];
	unordered_map<string,int>mp;
	for(auto s:S){
		for(int l=1;l<sz(s)-1;l++){
			for(int r=l+1;r<=sz(s)-1;r++){
				string t=s.substr(0,l)+to_string(r-l)+s.substr(r,sz(s)-r);
				mp[t]+=1;
			}
		}
	}
	
	int ans=0;
	for(auto s:S){
		int mx=0;
		for(int l=1;l<sz(s)-1;l++){
			for(int r=l+1;r<=sz(s)-1;r++){
				string t=s.substr(0,l)+to_string(r-l)+s.substr(r,sz(s)-r);
				if(mp[t]==1)mx=max(mx,r-l);
			}
		}
		ans+=mx;
	}
	cout<<ans<<endl;
	return true;
}

int main(){
	while(solve());
	return 0;
}
accuracy: 1
time_ms: 640
memory_kb: 47368
final_rank: 8

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<int(n);i++)
#define sz(a) int(a.size())

bool solve(){
	int N;cin>>N;
	if(N==0)return 0;

	vector<string>S(N);
	rep(i,N) cin>>S[i];
	unordered_map<string,int>mp;
	for(auto s:S){
		for(int l=1;l<sz(s)-1;l++){
			for(int r=l+1;r<=sz(s)-1;r++){
				string t=s.substr(0,l)+to_string(r-l)+s.substr(r,sz(s)-r);
				mp[t]+=1;
			}
		}
	}
	int ans=0;
	for(auto s:S){
		int mx=0;
		for(int l=1;l<sz(s)-1;l++){
			for(int r=l+1;r<=sz(s)-1;r++){
				string t=s.substr(0,l)+to_string(r-l)+s.substr(r,sz(s)-r);
				if(mp[t]==1)mx=max(mx,r-l);
			}
		}
		ans+=mx;
	}
	cout<<ans<<endl;
	return 1;
}

int main(){
	while(solve());
	return 0;
}
accuracy: 1
time_ms: 650
memory_kb: 47396
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<int(n);i++)
#define sz(a) int(a.size())

bool solve(){
	int N;cin>>N;
	if(N==0)return 0;

	vector<string>S(N);
	rep(i,N) cin>>S[i];
	unordered_map<string,int>mp;
	for(auto s:S){
		mp[s]+=1;
		for(int l=1;l<sz(s)-1;l++){
			for(int r=l+1;r<=sz(s)-1;r++){
				string t=s.substr(0,l)+to_string(r-l)+s.substr(r,sz(s)-r);
				mp[t]+=1;
			}
		}
	}
	int ans=0;
	for(auto s:S){
		int mx=0;
		for(int l=1;l<sz(s)-1;l++){
			for(int r=l+1;r<=sz(s)-1;r++){
				string t=s.substr(0,l)+to_string(r-l)+s.substr(r,sz(s)-r);
				if(mp[t]==1)mx=max(mx,r-l);
			}
		}
		ans+=mx;
	}
	cout<<ans<<endl;
	return 1;
}

int main(){
	while(solve());
	return 0;
}
accuracy: 1
time_ms: 630
memory_kb: 47424
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define ALL(v) v.begin(),v.end()
#define dbg(x) cerr << #x << ": " << (x) << endl;
int N;
vector<string>S;
int solve() {
    cin >> N;
    S.resize(N);
    if (N==0) exit(0);
    for (auto& s : S) cin >> s;

    map<tuple<int,string,string>, vector<int>> mp;
    for (int i = 0; i < N; ++i) {
        int M = S[i].size();
        auto& s = S[i];
        for (int w = 1; w+2 <= M; ++w) {
            for (int l = 1; l+w < M; ++l) {
                int r = l + w;
                mp[{w,s.substr(0,l), s.substr(r)}].push_back(i);
            }
        }
        mp[{0,s,""}].push_back(i);
    }
    vector<bool> used(N);
    int ans=0;
    for (auto it = mp.rbegin(); it != mp.rend(); ++it) {
        auto& [w,h,t] = it->first;
        auto& v = it->second;
        // cerr << w << " " << h << " " << t << endl;
        if (v.size() == 1 && !used[v.front()]) {
            ans += w;
            used[v.front()] = true;
        }
    }
    return ans;
}
int main() {
    while (true) {
        cout << solve() << '\n';
    }
}
accuracy: 1
time_ms: 600
memory_kb: 79228
final_rank: 12

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

#include <unistd.h>

namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf


uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
    char num[10000][4];
    constexpr Pre() : num() {
        for (int i = 0; i < 10000; i++) {
            int n = i;
            for (int j = 3; j >= 0; j--) {
                num[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;

inline void load() {
    memmove(ibuf, ibuf + pil, pir - pil);
    pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
    pil = 0;
    if (pir < SZ)
        ibuf[pir++] = '\n';
}

inline void flush() {
    fwrite(obuf, 1, por, stdout);
    por = 0;
}

void rd(char &c) {
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
}

void rd(string &x) {
    x.clear();
    char c;
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
    do {
        x += c;
        if (pil == pir)
            load();
        c = ibuf[pil++];
    } while (!isspace(c));
}

template <typename T> void rd_real(T &x) {
    string s;
    rd(s);
    x = stod(s);
}

template <typename T> void rd_integer(T &x) {
    if (pil + 100 > pir)
        load();
    char c;
    do
        c = ibuf[pil++];
    while (c < '-');
    bool minus = 0;
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (c == '-') {
            minus = 1, c = ibuf[pil++];
        }
    }
    x = 0;
    while ('0' <= c) {
        x = x * 10 + (c & 15), c = ibuf[pil++];
    }
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (minus)
            x = -x;
    }
}

void rd(int &x) {
    rd_integer(x);
}
void rd(ll &x) {
    rd_integer(x);
}
void rd(i128 &x) {
    rd_integer(x);
}
void rd(uint &x) {
    rd_integer(x);
}
void rd(ull &x) {
    rd_integer(x);
}
void rd(u128 &x) {
    rd_integer(x);
}
void rd(double &x) {
    rd_real(x);
}
void rd(long double &x) {
    rd_real(x);
}

template <class T, class U> void rd(pair<T, U> &p) {
    return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T> void rd_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        rd(x);
        rd_tuple<N + 1>(t);
    }
}
template <class... T> void rd(tuple<T...> &tpl) {
    rd_tuple(tpl);
}

template <size_t N = 0, typename T> void rd(array<T, N> &x) {
    for (auto &d : x)
        rd(d);
}
template <class T> void rd(vector<T> &x) {
    for (auto &d : x)
        rd(d);
}

void read() {}
template <class H, class... T> void read(H &h, T &...t) {
    rd(h), read(t...);
}

void wt(const char c) {
    if (por == SZ)
        flush();
    obuf[por++] = c;
}
void wt(const string s) {
    for (char c : s)
        wt(c);
}
void wt(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++)
        wt(s[i]);
}

template <typename T> void wt_integer(T x) {
    if (por > SZ - 100)
        flush();
    if (x < 0) {
        obuf[por++] = '-', x = -x;
    }
    int outi;
    for (outi = 96; x >= 10000; outi -= 4) {
        memcpy(out + outi, pre.num[x % 10000], 4);
        x /= 10000;
    }
    if (x >= 1000) {
        memcpy(obuf + por, pre.num[x], 4);
        por += 4;
    } else if (x >= 100) {
        memcpy(obuf + por, pre.num[x] + 1, 3);
        por += 3;
    } else if (x >= 10) {
        int q = (x * 103) >> 10;
        obuf[por] = q | '0';
        obuf[por + 1] = (x - q * 10) | '0';
        por += 2;
    } else
        obuf[por++] = x | '0';
    memcpy(obuf + por, out + outi + 4, 96 - outi);
    por += 96 - outi;
}

template <typename T> void wt_real(T x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << double(x);
    string s = oss.str();
    wt(s);
}

void wt(int x) {
    wt_integer(x);
}
void wt(ll x) {
    wt_integer(x);
}
void wt(i128 x) {
    wt_integer(x);
}
void wt(uint x) {
    wt_integer(x);
}
void wt(ull x) {
    wt_integer(x);
}
void wt(u128 x) {
    wt_integer(x);
}
void wt(double x) {
    wt_real(x);
}
void wt(long double x) {
    wt_real(x);
}

template <class T, class U> void wt(const pair<T, U> val) {
    wt(val.first);
    wt(' ');
    wt(val.second);
}
template <size_t N = 0, typename T> void wt_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) {
            wt(' ');
        }
        const auto x = std::get<N>(t);
        wt(x);
        wt_tuple<N + 1>(t);
    }
}
template <class... T> void wt(tuple<T...> tpl) {
    wt_tuple(tpl);
}
template <class T, size_t S> void wt(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}
template <class T> void wt(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}

void print() {
    wt('\n');
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    wt(head);
    if (sizeof...(Tail))
        wt(' ');
    print(forward<Tail>(tail)...);
}
void __attribute__((destructor)) _d() {
    flush();
}
} // namespace fastio


using fastio::flush;
using fastio::print;
using fastio::read;

inline void first(bool i = true) {
    print(i ? "first" : "second");
}
inline void Alice(bool i = true) {
    print(i ? "Alice" : "Bob");
}
inline void Takahashi(bool i = true) {
    print(i ? "Takahashi" : "Aoki");
}
inline void yes(bool i = true) {
    print(i ? "yes" : "no");
}
inline void Yes(bool i = true) {
    print(i ? "Yes" : "No");
}
inline void No() {
    print("No");
}
inline void YES(bool i = true) {
    print(i ? "YES" : "NO");
}
inline void NO() {
    print("NO");
}
inline void Yay(bool i = true) {
    print(i ? "Yay!" : ":(");
}
inline void Possible(bool i = true) {
    print(i ? "Possible" : "Impossible");
}
inline void POSSIBLE(bool i = true) {
    print(i ? "POSSIBLE" : "IMPOSSIBLE");
}

/**
 * @brief Fast IO
 */

int main() {
while(1) {
    int N;
    cin >> N;
    if (N == 0) return 0;
    vector<string> S(N);
    rep(i,0,N) cin >> S[i];
    map<pair<pair<string,string>,int>,int> mp;
    rep(i,0,N) {
        int L = S[i].size();
        rep(j,1,L) {
            rep(k,j+1,L) {
                pair<string,string> PS = {S[i].substr(0,j),S[i].substr(k,L-k)};
                pair<pair<string,string>,int> X = {PS,k-j};
                mp[X]++;
            }
        }
    }
    int ANS = 0;
    rep(i,0,N) {
        int L = S[i].size();
        int MAX  = 0;
        rep(j,1,L) {
            rep(k,j+1,L) {
                pair<string,string> PS = {S[i].substr(0,j),S[i].substr(k,L-k)};
                pair<pair<string,string>,int> X = {PS,k-j};
                if (mp[X] == 1) chmax(MAX,k-j);
            }
        }
        ANS += MAX;
    }
    cout << ANS << endl;
}
}
accuracy: 1
time_ms: 910
memory_kb: 60152
final_rank: 14