10602 - Editor Nottoobad
Posted: Thu Jan 29, 2004 9:53 am
Can anybody give me some hint
thx.
thx.
Could you give more hints?Dmytro Chernysh wrote:Sorting...
algorithmDmytro Chernysh wrote:Who or what? Me or the algorithm?
I also use greedy and get wrong answer.titid_gede wrote:greedy
I don't understand your meaning clearly, could you talk more detail ?horape wrote:Cost is the number of keys should need to type, so better not to delete any letter you'll need later. You want to group the words that have common letters at the start. Sort by # of common letters with the first word and then lexicographically (sp?)
Saludos,
HoraPe
Let's see if an example makes it clearer:windows2k wrote:I don't understand your meaning clearly, could you talk more detail ?horape wrote:Cost is the number of keys should need to type, so better not to delete any letter you'll need later. You want to group the words that have common letters at the start. Sort by # of common letters with the first word and then lexicographically (sp?)
Saludos,
HoraPe
Code: Select all
abcdef
aasda 1
aafgh 1
abcjkl 3
ghy 0
aafyy 1
abcghj 3
uiop 0
Code: Select all
abcdef
abcjkl 3
abcghj 3
aafgh 1
aafyy 1
aasda 1
ghy 0
uiop 0
Code: Select all
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct T
{
char str[105];
int value;
}s[105];
int sfunc(void const *a,void const *b)
{
T p,q;
int l1,l2;
p=*(T *)a;
q=*(T *)b;
l1=strlen(p.str);
l2=strlen(q.str);
if(p.value==q.value)
{
if(l1==l2) return (strcmp(q.str,p.str));
return (l2-l1);
}
return (q.value-p.value);
}
void main()
{
int testcase,N,i,l,l1,l2,k1,k2,c,count;
char str[105];
//freopen("10602.in","r",stdin);
scanf("%d",&testcase);
while(testcase--)
{
scanf("%d",&N);
scanf("%s",str);
l=strlen(str);
strcpy(s[0].str,str);
s[0].value=l;
for(i=1;i<N;i++)
{
scanf("%s",str);
strcpy(s[i].str,str);
l1=strlen(str);
k1=k2=count=0;
while((s[0].str[k1]==s[i].str[k2]) && k1<l && k2<l1)
{
count++;
k1++;
k2++;
}
s[i].value=count;
}
qsort(s,N,sizeof(s[0]),sfunc);
count=s[0].value;
for(i=1;i<N;i++)
{
l1=strlen(s[i-1].str);
l2=strlen(s[i].str);
k1=k2=c=0;
while((s[i-1].str[k1]==s[i].str[k2]) && k1<l1 && k2<l2)
{
k1++;
k2++;
c++;
}
count+=(l2-c);
}
printf("%d\n",count);
for(i=0;i<N;i++) printf("%s\n",s[i].str);
}
}
output:1
2
ab
abc