10815 - Andy's First Dictionary
Moderator: Board moderators
10815 - Andy's First Dictionary
Problem description says that number of distinct words will be less than 5000. But you're assuming total number of words to be less than 5000.
10815 wrong
Who can help me solve this problem, thanks~
Code: Select all
#include<iostream>
#include<string>
#include<set>
using namespace std;
main()
{
set<string> words;
string buffer="", word="";
while( cin >> buffer ) {
int len = buffer.length();
for(int i=0; i<=len; ++i) {
if( buffer[i] >= 'A' && buffer[i] <= 'Z' )
word+=tolower(buffer[i]);
else if( buffer[i] >= 'a' && buffer[i] <= 'z' )
word+=buffer[i];
else if( word != "" ) {
words.insert(word);
word="";
}
}
}
set<string>::iterator p;
for(p=words.begin(); p!=words.end(); ++p)
cout << *p << endl;
}
-
- A great helper
- Posts: 481
- Joined: Sun Jun 19, 2005 1:18 am
- Location: European Union (Slovak Republic)
Re: 10815 wrong
Shouldn't it be i < len ?watershed wrote:Code: Select all
int len = buffer.length(); for(int i=0; i<=len; ++i) {
I got WA too :'(
Code: Select all
ACed
Last edited by FAQ on Sun Aug 14, 2005 4:06 pm, edited 3 times in total.
-
- A great helper
- Posts: 481
- Joined: Sun Jun 19, 2005 1:18 am
- Location: European Union (Slovak Republic)
Re: I got WA too :'(
Have you tried any really huge input? What about 5000 random words of length something about 200 chars?
-
- A great helper
- Posts: 481
- Joined: Sun Jun 19, 2005 1:18 am
- Location: European Union (Slovak Republic)
Limits. Re: I got WA too :'(
Greetings FAQ!
For this problem I considered all words to be <= than 200 chars long, and no more than 50000 words in Andy's Dictionary (maybe a lot less than that would work).
Quick Sort did the work, and just don't print the repeated words.
Remember all words must be lowercase.
Ah!, and either tokenize lines, or build words char-by-char.
Hope it helps.
For this problem I considered all words to be <= than 200 chars long, and no more than 50000 words in Andy's Dictionary (maybe a lot less than that would work).
Quick Sort did the work, and just don't print the repeated words.
Remember all words must be lowercase.
Ah!, and either tokenize lines, or build words char-by-char.
Hope it helps.
_.
10815 RUNTIME ERROR!
Grrr....
runtime error!
runtime error!
Code: Select all
/*
10815 - andy's first dictionary
submission 1 runtime error! 2 output limit exceeded!
coded at 4:42 on 6th oct 05
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int sort_function(const void *a, const void *b) {
return strcmp((char *)a, (char *)b);
}
int main() {
//declaration
char line[205];
char words[5005][205];
char current_word[205];
int i,j,len,k;
int total=0;
while(1){
//input taking
while(gets(line)!=NULL) {
len=strlen(line);
//choosing word - lowering and then saving them
for(i=0;i<len;i++) {
j=0;
while(isalpha(line[i])) {
current_word[j]=line[i];
j++;i++;
}
current_word[j]=NULL;
for(k=0;k<j;k++)
current_word[k]=tolower(current_word[k]);
for(j=0;j<total;j++)
if(strcmp(current_word,words[j])==0)break;
if(j==total)
strcpy(words[total++],current_word);
}
}
//sorting the whole dictionary
qsort(words,total,sizeof(words[0]),sort_function);
//output routine
for(i=1;i<total;i++) {
//if(strcmp(words[i]," "));
if(words[i][0]!=' ')
printf("%s\n",words[i]);
}
}
return 0;
}
Last edited by smilitude on Wed Oct 12, 2005 2:44 am, edited 1 time in total.
fahim
#include <smile.h>
#include <smile.h>
10815 Presentation Error (PE)
Hey fellas,
I've been working on this problem for a while now. Finally got it to work correctly. The only problem now is that it's getting a presentation error. I bet it's something trivial, it's just that I can't see it. I tried putting/removing the newline at the end of the output file. That didn't make a difference. Can someone run this on their machine with the correct version of the compiler and let me know where I presented my data wrong? Thanks. This is what I have:
I've been working on this problem for a while now. Finally got it to work correctly. The only problem now is that it's getting a presentation error. I bet it's something trivial, it's just that I can't see it. I tried putting/removing the newline at the end of the output file. That didn't make a difference. Can someone run this on their machine with the correct version of the compiler and let me know where I presented my data wrong? Thanks. This is what I have:
Code: Select all
code removed...
Last edited by jjtse on Sun Dec 11, 2005 11:19 am, edited 1 time in total.
does anyone know what was wrong with watershed's code? Apparently, I'm having the same problem he used to have. I compared the output produce by my codes with the output produced by his codes, and it looks exactly the same. Mine, however, has presentation error instead of wrong answer, and I can't see where I'm presenting it wrong.
-
- A great helper
- Posts: 481
- Joined: Sun Jun 19, 2005 1:18 am
- Location: European Union (Slovak Republic)
Re: 10815 Presentation Error (PE)
If the input begins with a non-alpha character your output contains the empty word "".jjtse wrote:Code: Select all
while (!cin.eof()){ s = ""; while (isalpha(c)){ s.push_back(c); if (cin.eof()) break; c=cin.get(); } insert(s);