10815 - Andy's First Dictionary

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

Moderator: Board moderators

Postby sapnil » Thu Nov 01, 2007 4:32 am

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

Postby alamgir kabir » Thu Nov 01, 2007 1:01 pm

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

Postby aeiou » Mon Jun 30, 2008 9:22 am

Hi ,
Deleted
aeiou
New poster
 
Posts: 21
Joined: Wed May 07, 2008 11:32 am

Re: 10815 - Andy's First Dictionary

Postby newton » Sun Oct 19, 2008 10:23 pm

Why RE.
please any one check my small code

Code: Select all
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#define MAX 5001
using 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;
}


advanced thanks
User avatar
newton
Experienced poster
 
Posts: 162
Joined: Thu Jul 13, 2006 7:07 am
Location: Campus Area. Dhaka.Bangladesh

Re: 10815 - Andy's First Dictionary

Postby mf » Mon Oct 20, 2008 1:34 am

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

Postby Marcel Hernandez » Wed Sep 23, 2009 2:11 pm

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

Postby Jehad Uddin » Thu Oct 08, 2009 10:11 am

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))
{
 .......
}
Jehad Uddin
Learning poster
 
Posts: 74
Joined: Fri May 08, 2009 5:16 pm

Re: 10815 - Andy's First Dictionary

Postby sazzadcsedu » Fri May 07, 2010 7:43 pm

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++;
   }
}

sazzadcsedu
Experienced poster
 
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Location: narayangong,bangladesh.

Re: 10815 - Andy's First Dictionary

Postby shantanu18 » Sat Sep 04, 2010 7:00 am

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

Postby shaon_cse_cu08 » Thu Nov 11, 2010 4:36 pm

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.... :( :cry:
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

Postby Victor Pangaldus » Mon Nov 22, 2010 1:28 am

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

Postby BUET » Wed Dec 29, 2010 8:51 am

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

Postby kissu parina » Sun Feb 06, 2011 7:03 am

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

Postby cool » Thu Feb 17, 2011 5:39 am

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

Postby plamplam » Fri Jul 08, 2011 12:56 pm

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
User avatar
plamplam
Experienced poster
 
Posts: 151
Joined: Fri May 06, 2011 11:37 am

PreviousNext

Return to Volume CVIII

Who is online

Users browsing this forum: No registered users and 1 guest