## 10815 - Andy's First Dictionary

Moderator: Board moderators

To alamgir

Input this case:

Code: Select all
`     aaa     a`

>> your code given some garbage output

Thanks
Keep posting
Sapnil
"Dream Is The Key To Success"

@@@ Jony @@@
sapnil
Experienced poster

Posts: 106
Joined: Thu Apr 26, 2007 2:40 pm
Location: CSE-SUST

I have got it Acc

Code: Select all
`//code remove`

Thank you very much sapnil.
alamgir kabir
New poster

Posts: 37
Joined: Wed Oct 03, 2007 10:42 am

### Re: 10815 - Andy's First Dictionary

Hi ,
Deleted
aeiou
New poster

Posts: 21
Joined: Wed May 07, 2008 11:32 am

### Re: 10815 - Andy's First Dictionary

Why RE.
please any one check my small code

Code: Select all
`#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>#define MAX 5001using namespace std;int comp(const void *a, const void *b){        return strcmp((char *)a,(char *)b);}int main(){   char str[2000],*p;   char word[MAX][100];   char dil[] = " .:,\"'";   int k = 0,i,j;   //freopen("in.txt","rt",stdin);   while(scanf("%s",str) == 1){      p = strtok(str,dil);      if(p)         strcpy(word[k++],p);      while(p){         p = strtok(NULL,dil);         if(p)                     strcpy(word[k++],p);      }         }   for(i = 0; i<k; i++){      for(j = 0;word[i][j]; j++){         word[i][j] = tolower(word[i][j]);      }   }   qsort(word,k,sizeof(word[0]),comp);   for(i = 1; i < k+1; i++){      if(strcmp(word[i],word[i-1]))         printf("%s\n",word[i-1]);   }   return 0;}`

newton
Experienced poster

Posts: 162
Joined: Thu Jul 13, 2006 7:07 am

### Re: 10815 - Andy's First Dictionary

char word[MAX][100];

I think words are allowed to be up to 200 characters long by problem's text. And since it seems like you're using C++ anyway, why not just use std::string to keep strings?

Then, the problem text says "You can be sure that he number of distinct words in the text does not exceed 5000." But you're assuming that the *total* number of words is at most 5001, which is wrong.

char dil[] = " .:,\"'";

That's not going to solve the problem.

Stick to definitions, "a word is defined as a consecutive sequence of alphabets".
mf
Guru

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

### Re: 10815 - Andy's First Dictionary

oh hai,

I've already solved the problem (scoring a pitiful run time btw) but I'm always not really sure of what to do when it comes to handle input like the one seen in this problem.

This is some kind of sketch written in C++ showing my approach:

Code: Select all
`    string word;    vector<string> dic;    char symbol = getchar();    while (symbol != EOF)    {           if (isalpha(symbol)) // convert 'symbol' to lowercase if necessary and append it to the end of                             // 'word'       else // add 'word' to the vector and sort it if 'word' is not in it yet and finally clear the             // auxiliary string       symbol = getchar();           }`

So I'd just like to know if there are better/easier/simpler ways to read several strings from a line. C code is also welcomed.
If only while (cin>> s) { ... } worked as if they were integers... -_-
Marcel Hernandez
New poster

Posts: 7
Joined: Wed Mar 04, 2009 4:00 pm
Location: Barcelona, Spain

### Re: 10815 - Andy's First Dictionary

you can use getline or gets
Code: Select all
`string str;while(getline(cin,str,'\n')){ .......then just take words ....}char str[300];while(gets(str)){ .......}`
Learning poster

Posts: 74
Joined: Fri May 08, 2009 5:16 pm

### Re: 10815 - Andy's First Dictionary

Or you can use scanf in following way
Code: Select all
`char S[20000][300];char str[300];char s[300];while(scanf("%s",str)==1){   k=0;   word=0;   for(i=0;str[i];i++)   {               if(isalpha(str[i]))      {         ch=tolower(str[i]);         s[k]=ch;         k++;         word=1;      }      else      {         if(word==1)         {            s[k]='\0';            strcpy(S[l],s);            l++;            word=0;            k=0;         }      }   }   if(word==1)   {       s[k]='\0';      strcpy(S[l],s);      l++;   }}`
Experienced poster

Posts: 136
Joined: Sat Nov 29, 2008 8:01 am

### Re: 10815 - Andy's First Dictionary

I think the following approach is good to take input for this problem. It is difficult to include all punctuation.
Code: Select all
`while((getline(cin,st))!=NULL){   strcpy(ch,st.c_str());   for(int i=0;i<(int)strlen(ch);i++)   {      if(!isalpha(ch[i]))         ch[i]=' ';      else if(isalpha(ch[i]) && isupper(ch[i])) ch[i]=tolower(ch[i]); //making all lower case   }   parse(ch);}//parse the char arr using strtok. there are only space char and tab char. Now it is easy to parse `
shantanu18
New poster

Posts: 22
Joined: Tue Jul 20, 2010 9:55 pm

### Re: 10815 - Andy's First Dictionary

Plz Help me I m getting RE....
Code: Select all
`#include<stdio.h>#include<string.h>void ch_sort(char str[5000][201],int n);int main(){   int n=0,i,j,tag=1;   char a[5000][201],b[5000][201];   while(gets(a[n++]))   for(i=0;i<n;i++)   {       for(j=0;a[i][j]!='\0';j++)      {         if(a[i][j]>='A'&&a[i][j]<='Z')            a[i][j]+=32;      }   }      int k=0,l;   for(i=0;i<n;i++)   {      l=0;      tag=0;       for(j=0;a[i][j]!='\0';j++)      {         if(a[i][j]>='a'&&a[i][j]<='z')         {            b[k][l++]=a[i][j];            tag=1;         }         else         {            if(tag==1)            {               b[k][l]='\0';               k++;               l=0;               tag=0;            }         }            }      if(tag==1)      {         b[k][l]='\0';         k++;         l=0;         tag=0;      }   }      ch_sort(b,k);   for(i=0;i<k;i++)   {      if(b[i][0]!='\0')         puts(b[i]);   }      return 0;}void ch_sort(char str[5000][201],int n){   char dummy[201];      int i,j;   for(i=0;i<n-1;i++)   {      for(j=0;j<=n-i-1;j++)      {         if(strcmp(str[j],str[j+1])>0)         {                     strcpy(dummy,str[j]);            strcpy(str[j],str[j+1]);            strcpy(str[j+1],dummy);         }         if(strcmp(str[j],str[j+1])==0)            str[j+1][0]='\0';      }   }return;}`

Plz plz Help....
It's not who i m inside me... But what i do, that defines me...:)
shaon_cse_cu08
New poster

Posts: 46
Joined: Tue May 25, 2010 9:10 am

### Re: 10815 - Andy's First Dictionary

Hi all,

I am newbie to Uva online judge. I am stuck on this problem (10815: Andy's First Dictionary) . My code give right outputs for the test case I get from the problem and in this forum but I continually get WA for this problem. I do not know what is wrong with my code. Can anyone help me to figure it out by giving me some critical test case?

I am looking forward to hearing from you. Thank you in advanced.

Regards,

Victor Pangaldus
Victor Pangaldus
New poster

Posts: 2
Joined: Tue Sep 21, 2010 11:34 pm

### 10815 - Andy's First Dictionary

My accepte program uses these delimiters:

" !\"#\$%&'()*+,-./0123456789:;<=>?@[\\]^_`{|}~\t\r\n"
BUET
New poster

Posts: 19
Joined: Sun Jun 13, 2010 8:38 am

### Re: 10815 - Andy's First Dictionary

easy problem...just keep in mind that
andy's-->>
andy
s

not andys.....
one day...
kissu parina
New poster

Posts: 19
Joined: Thu May 20, 2010 8:58 am

### Re: 10815 - Andy's First Dictionary

can any one help me please

i am getting TLE
cool
New poster

Posts: 2
Joined: Mon Feb 14, 2011 6:45 am

### Re: 10815 - Andy's First Dictionary

Use a faster sorting algorithm (I think you are using bubble-sort) to sort the words in dictionary order. I used qsort to get AC in 0.115 seconds. Good luck.
You tried your best and you failed miserably. The lesson is 'never try'. -Homer Simpson

plamplam
Experienced poster

Posts: 151
Joined: Fri May 06, 2011 11:37 am

PreviousNext