739 - Soundex Indexing

All about problems in Volume 7. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Leaf
New poster
Posts: 1
Joined: Mon May 20, 2002 10:56 am
Location: Toronto, Ontario, Canada
Contact:

739 - Soundex Indexing

Post by Leaf » Mon May 20, 2002 11:00 am

Would you please tell me why I got wrong answers?


[c]
#include <stdio.h>

const int map[26] =
{ 0,1,2,3,0,
1,2,0,0,2,
2,4,5,5,0,
1,2,6,2,3,
0,1,0,2,0,2};

void mainf()
{
char s[1000];
int c;
int i;

printf("%9sName%21sSOUNDEX CODE\n","","");
while (1)
{
if (scanf("%s",&s)!=1)
{
printf("%19sEND OF OUTPUT\n");
return;
}

while ( (strlen(s)>0) && (s[0]==' ') )
memmove(s,s[1],sizeof(s)-sizeof(s[0]));
while ( (strlen(s)>0) && (s[strlen(s)-1]==' ') )
s[strlen(s)-1]=0;

if (strcmp(s,"")==0)
{
printf("%19sEND OF OUTPUT\n","");
return;
}

printf("%9s%-25s","",s);
printf("%c",s[0]);

c=0;
for (i=1; i<=strlen(s)-1; i++)
if (map[s-'A']>0)
{
if ( (c<3) && (map[s-'A']!=map[s[i-1]-'A']) )
{
c++;
printf("%d",map[s-'A']);
}
}

for (i=1; i<=3-c; i++) printf("0");
printf("\n");
}
}

void main()
{/*
freopen("soundex.in","r",stdin);
freopen("soundex.out","w",stdout);*/

mainf();
/*
fclose(stdout);
fclose(stdin);*/
}[/c]

Spike
New poster
Posts: 29
Joined: Mon Mar 18, 2002 2:00 am
Location: Washington State
Contact:

Post by Spike » Tue Dec 17, 2002 2:14 pm

Yeah, problem seems pretty straight forward, but I keep getting WA...

[java]
class Main{
public static void main( String[] args ){
//0123456789012345678901234567890123456789012345678901234567890123456789012345678901
String idx = "ABCDLMR***EPST*N****IFK*******OVG*******UJ********YQ********WX********HZ2345678901";
String cur = "";
int last = 0;
printSpaces( 9 );
System.out.print("NAME" );
printSpaces(21);
System.out.println("SOUNDEX CODE");
while( ( cur = token() ) != null ){
printSpaces(9);
System.out.print(cur);
String code = "" + cur.charAt(0);
last = idx.indexOf( cur.charAt(0) ) % 10;
for(int i = 1; i < cur.length(); i++){
int num = idx.indexOf( cur.charAt(i) ) % 10;
if( num != 0 && num != last ) code += "" + num;
last = num;
}
code += "000";
printSpaces( 25-cur.length() );
System.out.println( code.substring(0,4) );
}
printSpaces(19);
System.out.println("END OF OUTPUT");
}

static void printSpaces( int x ){
for( ; x > 0; x-- ) System.out.print(" " );
}

static String readLine(){return token( "\n" );}
static String token( ){return token( " \n\r\t" );}
static String token( String delim ){
char c = delim.charAt(0);
StringBuffer s = new StringBuffer("");
try{
while( delim.indexOf( (int) c ) != -1 && c != 65535 )
c = (char) System.in.read();
while( delim.indexOf( (int) c ) == -1 && c != 65535 ){
s.append( (char) c );
c = (char) System.in.read();
}
}catch( Exception e ){ return (null); }
if( s.toString().equals("") ) return null;
return s.toString();
}
}[/java]

deddy one
Experienced poster
Posts: 120
Joined: Tue Nov 12, 2002 7:36 pm

Post by deddy one » Fri Jan 03, 2003 6:52 am

I think that if you can pass the sample input then 90 % you'll get
ACC , remember that succesive sequence is counted from the first letter

also printf end of line when you're done printing the end of output


good luck.

sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

739

Post by sumankar » Fri Apr 04, 2003 8:27 pm

Hi
Leaf:chked ur code
1)the first place
-->line 21: printf("%19sEND OF OUTPUT\n"),
u should
add "" after the fmt string in printf
2)silly Name -->NAME,
just above err1
hope this helps
suman

eXon
New poster
Posts: 17
Joined: Sun Mar 23, 2003 2:04 pm
Location: Russia
Contact:

739

Post by eXon » Tue Apr 29, 2003 6:23 am

:( I have some problems with my code (This problem is WA). I think the reason for it is that i don't understand problem completely. Could somebody answer me which soundex codes corresponds to the following input names:
CAC
CCCCCCc
BAPBAPBAP
LAMALAN
CCAC
ZITA

Or check my Solve routine.
[cpp]
#include <stdlib.h>
#include <ctype.h>
#include <fstream.h>
#include <iomanip.h>
#include <string.h>


int finish = 0; // finish flag for multiple input
long caseNo;
#ifdef ONLINE_JUDGE
const long MAX = 30;
#else
const long MAX = 30;
#endif

char word[MAX], res[MAX];
int ind = 0;

void Input(){
finish = cin.eof();
if (finish)
return;
cin >> word;
ind = 0;
res[0] = 0;
}

void add(char c){
res[ind++] = c;
res[ind] = 0;
}

// returns a soundex code corresponding to ``c''. If char is not
// encoded it returns 0
int soundex(char c){
switch(c){
case 'B':case 'P':case 'F':case 'V': return 1; break;
case 'C':case 'S':case 'K':case 'G':
case 'J':case 'Q':case 'X':case 'Z': return 2; break;
case 'D': case 'T': return 3; break;
case 'L': return 4; break;
case 'M': case 'N': return 5; break;
case 'R': return 6; break;
}
return 0;
}

void Solve(){
int lastcode = 0, currcode = 0;

add(word[0]);
lastcode = soundex(word[0]);
for (int i = 1; (i < strlen(word) && strlen(res) <= 3); i++){
if ((currcode = soundex(word))){ // if current letter is encoded
if (lastcode != currcode) // if lastcode not equals to current
add('0' + currcode); // adding current code
lastcode = currcode; // assigning lastcode
}
else
lastcode = 0;
}

// appending trailing zeros
int sl = 4 - strlen(res);

for (int j = 0; j < sl; j++)
add('0');
}

void Output()
{
cout << setw(9 + strlen(word)) << word
<< setw(25 + strlen(res) - strlen(word)) << res;
}

//#define DEBUG

int main(){
#ifndef ONLINE_JUDGE
ifstream is = "input.txt";
cin = is;
#ifdef DEBUG
clrscr();
cout << endl;
#else
ofstream os = "output.txt";
cout = os;
#endif
#endif

cout << setw(13) << "NAME" << setw(33) << "SOUNDEX CODE" << endl;
for (caseNo = 0; !finish; caseNo++){
Input();
if (finish)
break;
if (caseNo)
cout << endl;
Solve();
Output();
}
cout << endl << setw(32) << "END OF OUTPUT";
return 0;
}
//@END_OF_SOURCE_CODE
[/cpp]
Who am I? Just eXon...

Eric
Learning poster
Posts: 83
Joined: Wed Sep 11, 2002 6:28 pm
Location: Hong Kong

Post by Eric » Tue Apr 29, 2003 1:55 pm

My program gives

Code: Select all

         NAME                     SOUNDEX CODE
         CAC                      C200
         CCCCCCc                  C22c
         BAPBAPBAP                B111
         LAMALAN                  L545
         CCAC                     C200
         ZITA                     Z300
                   END OF OUTPUT
Although it has not got accepted yet :(
For the second input, I think it is invalid since the program states that there will only be upper case letter.

Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski » Wed Apr 30, 2003 7:36 am

My avvepted code prints:

Code: Select all


          NAME                     SOUNDEX CODE
          CAC                      C200
          CCCCCCc                  C000
          BAPBAPBAP                B111
          LAMALAN                  L545
          CCAC                     C200
          ZITA                     Z300
                   END OF OUTPUT
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)

Eric
Learning poster
Posts: 83
Joined: Wed Sep 11, 2002 6:28 pm
Location: Hong Kong

Post by Eric » Wed Apr 30, 2003 8:13 am

Thanks, Michniewski.
I overlook some important things in the problem.
I also get accepted now with the smae output with you.

eXon
New poster
Posts: 17
Joined: Sun Mar 23, 2003 2:04 pm
Location: Russia
Contact:

Post by eXon » Wed Apr 30, 2003 11:29 am

My code gives the same results as your accepted code, but I have WA. Could you check my Solve routine, please?! :o
Who am I? Just eXon...

eXon
New poster
Posts: 17
Joined: Sun Mar 23, 2003 2:04 pm
Location: Russia
Contact:

Where are everybody?!!!

Post by eXon » Sun May 04, 2003 4:21 pm

Please, answwer me!!!
Who am I? Just eXon...

zsepi
Learning poster
Posts: 51
Joined: Thu Sep 26, 2002 7:43 pm
Location: Easton, PA, USA

Post by zsepi » Wed Jul 02, 2003 12:03 am

hi,
i'm afraid a formatting problem keeps giving me WA... could someone check if it gives the output in the right format?
appreciated!
[c]main() {
char line[50], *name, *sx, *s; /* name is the name, sx is the soundex code */
int i, j;

for(i = 0; i < 50; i++)
line = ' ';
sx = &line[34];
name = &line[9];
sx[4] = '\0';
printf("%9sNAME%21sSOUNDEX CODE\n","","");
while(scanf("%s", name) == 1) {
/* generating the soundex code */
printf("%s\n", line);
}
printf("%19sEND OF OUTPUT\n", "");
}[/c]
Dealing with failure is easy: Work hard to improve.
Success is also easy to handle: You've solved the wrong problem. Work hard to improve.

User avatar
Jemerson
Learning poster
Posts: 59
Joined: Mon Feb 02, 2004 11:19 pm
Contact:

739 Soundex Indexing - WA - CAN'T FIND MY MISTAKE

Post by Jemerson » Fri Jun 10, 2005 9:20 pm

Can anyone who has solves this problem please check my input/output?

INPUT:

Code: Select all

LEE
KUHNE
EBELL
EBELSON
SCHAEFER
SCHAAK
BPFVCSKGJ
BCDT
BCDR
BBBBB
CCCCC
LLLLLLMNR
BPFVVVVVVVVBPPPPF
CBGJQXZZZZZ
BCSDTLMRBCSM
OUTPUT:

Code: Select all

          NAME                     SOUNDEX CODE
          LEE                      L000
          KUHNE                    K500
          EBELL                    E140
          EBELSON                  E142
          SCHAEFER                 S160
          SCHAAK                   S200
          BPFVCSKGJ                B200
          BCDT                     B230
          BCDR                     B236
          BBBBB                    B000
          CCCCC                    C000
          LLLLLLMNR                L560
          BPFVVVVVVVVBPPPPF        B000
          CBGJQXZZZZZ              C120

          BCSDTLMRBCSM             B234


So, is this correct? is there any special case? Help, please
UFCG Brazil - Computer Science graduate student
http://acm.uva.es/problemset/usersnew.php?user=54806 ... and going up!

daveon
Experienced poster
Posts: 229
Joined: Tue Aug 31, 2004 2:41 am
Location: TORONTO, CANADA

Post by daveon » Sun Aug 14, 2005 9:33 pm

Your output seems right except for that blank line.

Did you forgot to output END OF OUTPUT ?

User avatar
Roby
Experienced poster
Posts: 101
Joined: Wed May 04, 2005 4:33 pm
Location: Tangerang, Banten, Indonesia
Contact:

739 - WA

Post by Roby » Fri Nov 04, 2005 11:09 am

Hi, everyone...
Can you tell me where's my mistake? I couldn't find it. Here's my code:

Code: Select all

CUT !!!
My program output same with the sample output. Please help me... :(
What a stupid mistake i made...
already solved...

Munni
New poster
Posts: 9
Joined: Tue Jan 03, 2006 4:59 pm

739 CE plz hlp..............

Post by Munni » Tue Jan 03, 2006 5:20 pm

I hv tested all possble cases , but it is always giving compile error.plz hlp me.

/*Here is my code*/
#include<stdio.h>
#include<iostream.h>
#include<string.h>
char compare(char ch)
{

if((ch=='B')||(ch=='P')||(ch=='F')||(ch=='V'))
return '1';


if((ch=='C')||(ch=='S')||(ch=='K')||(ch=='G')||(ch=='J')||(ch=='Q')||(ch=='X')||(ch=='Z'))
return '2';


if((ch=='D')||(ch=='T'))
return '3';

if((ch=='L'))
return '4';


if((ch=='M')||(ch=='N'))
return '5';

if((ch=='R'))
return '6';


return '0';




}

int main(void)
{

char ch,str2[5],ch1=' ',ch2;
char tmp=' ';
char str1[30];
int num=0,j,k;
for(int i=0;i<10;i++)
cout<<ch1;
cout<<"NAME";
for(i=0;i<21;i++)
cout<<ch1;
cout<<"SOUNDEX CODE";
cout<<"\n";
while((ch=getchar())!=EOF)

{
if(ch=='\n')
{
strcpy(str2,"0000");
ch2=' ';
tmp=' ';
str1[num]='\0';
for(i=0;i<10;i++)
cout<<ch1;
cout<<str1;
for(i=0;i<25-num;i++)
cout<<ch1;
j=0;
k=0;
str2[j]=str1[k];
ch2=compare(str1[0]);
tmp=ch2;
j++;
k++;
while((j<=3)&&(k<num))
{

ch2=compare(str1[k]);
if(tmp!=ch2)
{
str2[j]=ch2;
if(ch2!='0')
j++;
tmp=ch2;
k++;

}
else
k++;

}
str2[4]='\0';
cout<<str2;
num=0;
cout<<"\n";

}

else
{

str1[num]=ch;
num++;

}


}

for(i=0;i<20;i++)
cout<<ch1;
cout<<"END OF OUTPUT";
cout<<"\n";








return 0;


}

Post Reply

Return to “Volume 7 (700-799)”