对于每组数据,输出所有人名字的字母总数。
Sample Input 1 3 aaaaa bbb abababab Sample Output 5
一开始我用的是结构体来写,然后发现超时了,后来发现直接用string就可以实现
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define N 1000000+10 #define INF 0x3f3f3f3f using namespace std; /*struct node { char xx[N]; }Q[1010];*/ int main() { int repeat; int i,j; scanf("%d",&repeat); string xx[1010]; while(repeat--) { int n,sum=0; scanf("%d",&n); for(i=0;i<n;i++){ cin>>xx[i]; } sort(xx,xx+n); int temp = 1; int flag = 1; for(i=0;i<n-1;i++) { // int count1=-INF; for(j=0;j<xx[i].length()-1||j<xx[i+1].length()-1;j++) { if(xx[i][j]!=xx[i+1][j]) break; //if(i==j) // continue; /*for(int k=0;k<strlen(Q[i].xx);k++) { if(Q[i].xx[k]==Q[j].xx[k]) temp++; else{ temp++; //printf("%d\n",temp); break; } } if(count1<=temp) count1 = temp;*/ } temp=j+1; if(flag<temp) sum += temp; else sum += flag; flag = temp; } sum+=temp; printf("%d\n",sum); } return 0; }