10815 - Andy's First Dictionary

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

Moderator: Board moderators

bliscosque
New poster
Posts: 3
Joined: Mon Apr 25, 2005 12:33 am

10815 - Why WA?

Post by bliscosque » Mon Apr 25, 2005 12:37 am

Why does my code receive WA??


#include <ctype.h>
#include <iostream>
#include <fstream>
#include<algorithm>
#include <vector>
#include <string>
#include <set>

using namespace std;

void pri(string str) {
cout << str << endl;
}

string palavra;
string palavra2;
set<string> lista;
int num=0;
int main() {
while (cin >> palavra) {
int i=0;
int t=palavra.size();
palavra2.erase();
for (i=0;i<t;i++) {
char l=palavra.at(i);
if (l>='a' && l<='z')
palavra2+=l;
else if (l>='A' && l<='Z')
palavra2+=tolower(l);
}
lista.insert(palavra2);
}
for_each(lista.begin(),lista.end(), pri);
return 0;
}

Thank

misof
A great helper
Posts: 430
Joined: Wed Jun 09, 2004 1:31 pm

Post by misof » Mon Apr 25, 2005 1:33 am

Because it's wrong :P

Please DON'T just post unexplained code. NEVER. Nobody has the time to guess what is your code trying to do. When asking for help, always explain the idea of your algorithm. If there already is a thread on the problem you are solving, use the old thread, DON'T start a new one.

That being said, your problem is that you don't understand the problem statement correctly. It says: "a word is defined as a consecutive sequence of alphabets [sic, read:letters], in upper and/or lower case". The word consecutive is important. The input "won't" contains 2 words.

bliscosque
New poster
Posts: 3
Joined: Mon Apr 25, 2005 12:33 am

Post by bliscosque » Mon Apr 25, 2005 2:49 pm

My program aren

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post by mf » Mon Apr 25, 2005 3:54 pm

Your code above just removes all non-alphabetical characters from the string.
So "won't" in the input is interpreted as a single word "wont". But it should be interpreted as two separate words: "won" and "t".

bliscosque
New poster
Posts: 3
Joined: Mon Apr 25, 2005 12:33 am

Post by bliscosque » Tue Apr 26, 2005 4:52 am

Thank you. Now I get AC!!!

User avatar
tnaires
New poster
Posts: 10
Joined: Mon Mar 07, 2005 6:22 pm
Location: Natal - RN / Brazil

10815, WA

Post by tnaires » Thu Apr 28, 2005 2:29 am

Hi everyone :D
I'm trying hard to solve the 10815, but I'm having nightmares with WAs :(
Here's my code:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define DELIMITATORS " \n\'\"\\|.,;:?!()[]{}+-*=_&%$#@<>/"

typedef struct list
{
	char p[201];
	struct list *next;
} *List;

List ConsList()
{
	List result;
	
	result = (List) malloc(sizeof(struct list));
	strcpy(result->p, "");
	result->next = NULL;
	return result;
}

void FreeList(List l)
{
	List itr, tmp;
	
	itr = l;
	
	while (itr)
	{
		tmp = itr;
		itr = itr->next;
		free(tmp);
	}
}

Lista AddWordist(List l, char s[201])
{
	List n, itr, prev;
	int cmp;
	
	if (strcmp(l->p, "") == 0)
	{
		strcpy(l->p, s);
		return l;
	}
	
	itr = l;
	prev = NULL;
	
	while (itr)
	{
		cmp = strcmp(itr->p, s);
		
		if (cmp == 0)
		{
			return l;
		}
		else if (cmp > 0)
		{
			n = ConsList();
			strcpy(n->p, s);
			n->next = itr;
			
			if (prev)
			{
				prev->next = n;
			}
			else
			{
				l = n;	
			}
			
			return l;
		}
		
		prev = itr;
		itr = itr->next;
	}
	
	n = ConsList();
	strcpy(n->p, s);
	prev->next = n;
	return l;
}

void PrintList(List l)
{
	List itr;
	
	itr = l;
	
	while (itr)
	{
		printf("%s\n", itr->p);
		itr = itr->next;
	}
}

void lower_case(char *word)
{
	char lower[27], upper[27];
	int i, j;
	
	strcpy(upper, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
	strcpy(lower, "abcdefghijklmnopqrstuvwxyz");
	
	for (i = 0; i < strlen(word); i++)
	{
		for (j = 0; j < 26; j++)
		{
			if (word[i] == upper[j])
			{
				word[i] = lower[j];
			}
		}
	}
}

int main()
{
	List l;
	char line[201], *p;
	
	l = ConsList();
	
	while (fgets(line, 200, stdin) != NULL)
	{
		p = strtok(line, DELIMITATORS);
		
		while (p != NULL)
		{
			lower_case(p);
			l = AddWordList(l, p);
			p = strtok(NULL, DELIMITATORS);
		}
	}
	
	PrintList(l);
	FreeList(l);
	return 0;
}
Thank you all, greetings.
Tarso Nunes Aires
Natal/RN - Brazil

User avatar
Cho
A great helper
Posts: 274
Joined: Wed Oct 20, 2004 11:51 pm
Location: Hong Kong

Post by Cho » Thu Apr 28, 2005 5:42 am

Before you posted, did you search for help in the forum?
Did you read this thread?
Did you read what misof replied to that thread?

User avatar
tnaires
New poster
Posts: 10
Joined: Mon Mar 07, 2005 6:22 pm
Location: Natal - RN / Brazil

Post by tnaires » Thu Apr 28, 2005 1:43 pm

Yes, I did. If you see my code, you'll know that it don't have the problem appointed by misof. But thanks, anyway.
Tarso Nunes Aires
Natal/RN - Brazil

User avatar
tnaires
New poster
Posts: 10
Joined: Mon Mar 07, 2005 6:22 pm
Location: Natal - RN / Brazil

Post by tnaires » Thu Apr 28, 2005 1:47 pm

tnaires wrote:()... it don't have (...)
It doesn't, sorry!
Greetings
Tarso Nunes Aires
Natal/RN - Brazil

Antonio Ocampo
Experienced poster
Posts: 131
Joined: Sat Jul 17, 2004 4:09 am
Location: Lima, Per

Post by Antonio Ocampo » Fri Apr 29, 2005 11:48 pm

hI tnaires

Code: Select all

In this problem, a word is defined as a consecutive sequence of alphabets, in upper and/or lower case. Words with only one letter are also to be considered. 
Regards

User avatar
tnaires
New poster
Posts: 10
Joined: Mon Mar 07, 2005 6:22 pm
Location: Natal - RN / Brazil

Post by tnaires » Sat Apr 30, 2005 2:26 pm

Hi Antonio!
The code also recognizes words with only one letter. But, numbers should be considered?
Greetings.
Tarso Nunes Aires
Natal/RN - Brazil

Antonio Ocampo
Experienced poster
Posts: 131
Joined: Sat Jul 17, 2004 4:09 am
Location: Lima, Per

Post by Antonio Ocampo » Thu May 05, 2005 9:19 pm

Hi tnaires

Code: Select all

..a word is defined as a consecutive sequence of alphabets, in upper and/or lower case.
Therefore the numbers shouldn

medv
Learning poster
Posts: 85
Joined: Sun Jul 14, 2002 1:17 pm

I used STL

Post by medv » Thu May 26, 2005 11:09 pm

If you want - send me your code, I will look at it and will help you

roticv
Learning poster
Posts: 63
Joined: Sat Dec 11, 2004 9:28 am

Post by roticv » Sun Jun 05, 2005 5:09 am

I did according to what was mentioned above, but I still keep getting WA.

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char str[250];
char dic[6000][256];
int cnt,i,flag;

int compare(const void *a, const void *b){
	return strcmp((char *)a,(char *)b);
}

void tolower(char *c){
	int ii;
	for (ii=0;c[ii]!=0;ii++){
		c[ii] = c[ii] | 0x20;
	}
	return;
}

int main(){
	char *pch;
	cnt = 0;
	while (gets(str)!=NULL){
		pch = strtok(str," :\\;,./\n\t\'\"`~!@#$%^&*-_+=1234567890");
		while (pch != NULL){
			tolower(pch);
			flag = 1;
			for (i=0;i<cnt;i++){
				if (strcmp(pch,&dic[i][0])==0){
					flag = 0;
					break;
				}
			}
			if (flag == 1){
				strcpy(&dic[cnt][0],pch);
				cnt++;
			}
			pch = strtok(0," :\\;,./\n\t\'\"`~!@#$%^&*-_+=1234567890");
		}
	}
	qsort(&dic[0][0],cnt,256,compare);
	for (i=0;i<cnt;i++)
		printf("%s\n",&dic[i][0]);
	return 0;
}


Jewel of DIU
New poster
Posts: 32
Joined: Thu Jul 31, 2003 6:21 am
Location: Daffodil Univ, Bangladesh
Contact:

10815: RTE Why?

Post by Jewel of DIU » Thu Jun 16, 2005 12:32 pm

Hai, I have got RTE(Invalid Memory Reference)
Can anyone tell me why my code got RTE?
Here is my code

Code: Select all

# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <ctype.h>

char dic[10000][205];
char inp[205];


int main()
{
	int l,i,j,n=0;
	char buff[205];
	
	while(scanf("%s",&inp)==1)
	{
                       PARSE WORD & SAVE IN DIC[];
	}
                 SORT (DIC[]);

	OUTPUT DISTINCT WORD
}
Last edited by Jewel of DIU on Sun Jun 19, 2005 9:29 am, edited 2 times in total.
Hate WA
Visit phpBB!

Post Reply

Return to “Volume 108 (10800-10899)”