Shortest Names
比赛时队友直接上的纯暴力,结果超时,我就想先排一下序,之后每次判断相邻的两个,实时更新每个字符串的喊出长度
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
#include <string>
#include <vector>
using namespace std;
char s[1100000];
int x[1010];
int main(){
int t;
scanf("%d",&t);
while(t--){
vector <string>a;
int n;
memset(x,0,sizeof(x));
scanf("%d",&n);
for (int i=0;i<n;i++){
scanf("%s",s);
a.push_back(s);
x[i]=0;
}
sort(a.begin(),a.end());
for (int i=1;i<n;i++){
int aa=a[i].size();
for (int j=0;j<aa;j++){
if (a[i][j]!=a[i-1][j]){
x[i-1]=max(x[i-1],j+1);
x[i]=j+1;
break;
}
}
}
int sum=0;
for (int i=0;i<n;i++){
sum+=x[i];
}
printf("%d\n",sum);
}
return 0;
}