468 - Key to Success
Posted: Sat Jul 10, 2004 10:40 am
Could someone give some critical case of input?
Code: Select all
for(i=0;i<26;i++)
Problem description says:This is weird, I submitted 2 versions of code, one treating uppercase and lowercase as different letters, and one treating upper and lower as same letter, but output them according to their case. Both get PE
Note: You have to print a newline even after the last test case (though it is not said).The outputs of two consecutive cases will be separated by a blank line.
Code: Select all
/*
468 - key to success
submission 1 runtime error 2 runtime error 3 WA 4 WA
coded at 5:08pm on 6th oct 05
comment: did i make it complex? i dont think so!
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 10000
int main() {
char not_encoded[MAX+1],encoded[MAX+1];
int i,j,len;
int tcases;
char flag=0;
struct{
char ch;
int occ;
}lower[52],higher[52],temp;
scanf("%d",&tcases);
for(;tcases>0;tcases--) {
scanf("%s",not_encoded);
scanf("%s",encoded);
len=strlen(not_encoded);
if(flag) printf("\n");
for(i=0;i<26;i++) {
lower[i].ch=i+'a';
higher[i].ch=i+'a';
higher[i].occ=0;
lower[i].occ=0;
}
for(i=26;i<52;i++) {
lower[i].ch=i+'A'-26;
higher[i].ch=i+'A'-26;
higher[i].occ=0;
lower[i].occ=0;
}
for(i=0;i<len;i++) {
if(not_encoded[i]>='a' && not_encoded[i]<='z')
lower[not_encoded[i]-'a'].occ++;
else if(isalpha(not_encoded[i]))
lower[not_encoded[i]-'A'+26].occ++;
}
for(i=0;i<51;i++)
for(j=i+1;j<52;j++)
if(lower[i].occ<lower[j].occ) {
temp=lower[i];
lower[i]=lower[j];
lower[j]=temp;
}
len=strlen(encoded);
for(i=0;i<len;i++) {
if(encoded[i]>='a' && encoded[i]<='z')
higher[encoded[i]-'a'].occ++;
else if(isalpha(not_encoded[i]))
higher[encoded[i]-'A'+26].occ++;
}
for(i=0;i<51;i++)
for(j=i+1;j<52;j++)
if(higher[i].occ<higher[j].occ) {
temp=higher[i];
higher[i]=higher[j];
higher[j]=temp;
}
for(i=0;;i++) {
if(higher[i].occ==0) break;
for(j=0;j<len;j++) {
if(higher[i].ch==encoded[j])
encoded[j]=lower[i].ch;
}
}
puts(encoded);
flag=1;
}
return 0;
}
Code: Select all
3
<- empty line
abacxbacac
qqqqqrrrrssstt
<- empty line
abacxbacac
qqqqqrrrrssstt
<- empty line
abacxbacac
qqqqqrrrrssstt
EOF
Code: Select all
aaaaaccccbbbxx
<- empty line
aaaaaccccbbbxx
<- empty line
aaaaaccccbbbxx
Code: Select all
scanf("%d", &test);
for(count = 0; count < test; ++count)
{
scanf(" %[^\n]", s);
scanf(" %[^\n]", p);
.........
.........
}