第一轮 C
最短的名字
Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Submit Status
Description
Download as PDF
Shortest Names
In a strange village, people have very long names. For example: aaaaa, bbb and abababab.
You see, it's very inconvenient to call a person, so people invented a good way: just call a prefix of the names. For example, if you want to call `aaaaa', you can call `aaa', because no other names start with `aaa'. However, you can't call `a', because two people's names start with `a'. The people in the village are smart enough to always call the shortest possible prefix. It is guaranteed that no name is a prefix of another name (as a result, no two names can be equal).
If someone in the village wants to call every person (including himself/herself) in the village exactly once, how many characters will he/she use?
Input
The first line contains T ( T$ \le$10), the number of test cases. Each test case begins with a line of one integer n ( 1$ \le$n$ \le$1000), the number of people in the village. Each of the following n lines contains a string consisting of lowercase letters, representing the name of a person. The sum of lengths of all the names in a test case does not exceed 1,000,000.
Output
For each test case, print the total number of characters needed.
Sample Input
1
3
aaaaa
bbb
abababab
Sample Output
5
Problemsetter: Rujia Liu, Special Thanks: Yiming Li, Feng Chen, Jane Alam Jan
/*************************************************************************
> File Name: c.cpp
> Author:yuan
> Mail:
> Created Time: 2014年11月09日 星期日 09时44分06秒
************************************************************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
string str[1005];
int t,n;
bool check(string s,int num)
{
int l=s.length();
for(int i=0;i<n;i++)
{
if(i==num) continue;
if(str[i].compare(0,l,s)==0) return 0;
}
return 1;
}
int main()
{
int ans;
scanf("%d",&t);
while(t--){
ans=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
cin>>str[i];
}
for(int i=0;i<n;i++)
for(int j=1;j<=str[i].length();j++)
{
string s=str[i].substr(0,j);
if(check(s,i)) {ans+=s.length();j=str[i].length()+1;}
}
printf("%d\n",ans);
}
return 0;
}

浙公网安备 33010602011771号