Page 2 of 8
10815 - Why WA?
Posted: Mon Apr 25, 2005 12:37 am
by bliscosque
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
Posted: Mon Apr 25, 2005 1:33 am
by misof
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.
Posted: Mon Apr 25, 2005 2:49 pm
by bliscosque
My program aren
Posted: Mon Apr 25, 2005 3:54 pm
by mf
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".
Posted: Tue Apr 26, 2005 4:52 am
by bliscosque
Thank you. Now I get AC!!!
10815, WA
Posted: Thu Apr 28, 2005 2:29 am
by tnaires
Hi everyone

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.
Posted: Thu Apr 28, 2005 5:42 am
by Cho
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?
Posted: Thu Apr 28, 2005 1:43 pm
by tnaires
Yes, I did. If you see my code, you'll know that it don't have the problem appointed by misof. But thanks, anyway.
Posted: Thu Apr 28, 2005 1:47 pm
by tnaires
tnaires wrote:()... it don't have (...)
It doesn't, sorry!
Greetings
Posted: Fri Apr 29, 2005 11:48 pm
by Antonio Ocampo
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
Posted: Sat Apr 30, 2005 2:26 pm
by tnaires
Hi Antonio!
The code also recognizes words with only one letter. But, numbers should be considered?
Greetings.
Posted: Thu May 05, 2005 9:19 pm
by Antonio Ocampo
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
I used STL
Posted: Thu May 26, 2005 11:09 pm
by medv
If you want - send me your code, I will look at it and will help you
Posted: Sun Jun 05, 2005 5:09 am
by roticv
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;
}
10815: RTE Why?
Posted: Thu Jun 16, 2005 12:32 pm
by Jewel of DIU
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
}