739 - Soundex Indexing
Moderator: Board moderators
-
- New poster
- Posts: 1
- Joined: Mon May 20, 2002 10:56 am
- Location: Toronto, Ontario, Canada
- Contact:
739 - Soundex Indexing
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]
[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]
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]
[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]
739
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
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
739

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...
My program gives
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.
Code: Select all
NAME SOUNDEX CODE
CAC C200
CCCCCCc C22c
BAPBAPBAP B111
LAMALAN L545
CCAC C200
ZITA Z300
END OF OUTPUT

For the second input, I think it is invalid since the program states that there will only be upper case letter.
-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact:
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)
Born from ashes - restarting counter of problems (800+ solved problems)
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]
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.
Success is also easy to handle: You've solved the wrong problem. Work hard to improve.
739 Soundex Indexing - WA - CAN'T FIND MY MISTAKE
Can anyone who has solves this problem please check my input/output?
INPUT:
OUTPUT:
So, is this correct? is there any special case? Help, please
INPUT:
Code: Select all
LEE
KUHNE
EBELL
EBELSON
SCHAEFER
SCHAAK
BPFVCSKGJ
BCDT
BCDR
BBBBB
CCCCC
LLLLLLMNR
BPFVVVVVVVVBPPPPF
CBGJQXZZZZZ
BCSDTLMRBCSM
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!
http://acm.uva.es/problemset/usersnew.php?user=54806 ... and going up!
-
- Experienced poster
- Posts: 101
- Joined: Wed May 04, 2005 4:33 pm
- Location: Tangerang, Banten, Indonesia
- Contact:
739 - WA
What a stupid mistake i made...Hi, everyone...
Can you tell me where's my mistake? I couldn't find it. Here's my code:My program output same with the sample output. Please help me...Code: Select all
CUT !!!
already solved...
739 CE plz hlp..............
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;
}