10815 - Andy's First Dictionary
Moderator: Board moderators
-
- New poster
- Posts: 3
- Joined: Mon Apr 25, 2005 12:33 am
10815 - Why WA?
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
#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
Because it's wrong
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.

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.
10815, WA
Hi everyone 
I'm trying hard to solve the 10815, but I'm having nightmares with WAs
Here's my code:
Thank you all, greetings.

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;
}
Tarso Nunes Aires
Natal/RN - Brazil
Natal/RN - Brazil
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?
Did you read this thread?
Did you read what misof replied to that thread?
-
- Experienced poster
- Posts: 131
- Joined: Sat Jul 17, 2004 4:09 am
- Location: Lima, Per
hI tnaires
Regards
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.
-
- Experienced poster
- Posts: 131
- Joined: Sat Jul 17, 2004 4:09 am
- Location: Lima, Per
Hi tnaires
Therefore the numbers shouldn
Code: Select all
..a word is defined as a consecutive sequence of alphabets, in upper and/or lower case.
I used STL
If you want - send me your code, I will look at it and will help you
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;
}
-
- New poster
- Posts: 32
- Joined: Thu Jul 31, 2003 6:21 am
- Location: Daffodil Univ, Bangladesh
- Contact:
10815: RTE Why?
Hai, I have got RTE(Invalid Memory Reference)
Can anyone tell me why my code got RTE?
Here is my code
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!
Visit phpBB!