10126 - Zipf's Law

All about problems in Volume CI. 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 shamim » Wed Mar 17, 2004 9:37 am

Here are two quotes from the problem statement.

Words are separated by non-letters. Capitalization should be ignored


For each test case, output the words which occur n times in the input text, one word per line, lower case, in alphabetical order.


Your code does not handle this, for the input :

    2
    AA AA
    EndOftext

The output should be:

    aa
:wink:
User avatar
shamim
A great helper
 
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Dhaka

Postby Jewel of DIU » Sat Mar 20, 2004 11:25 am

My program is now ok for ur test case. But i got wa again.
If u have enough time then pls look through my code.
Or if u can't then give me more test case.
Hate WA
Visit phpBB!
Jewel of DIU
New poster
 
Posts: 32
Joined: Thu Jul 31, 2003 6:21 am
Location: Daffodil Univ, Bangladesh

Postby shamim » Sat Mar 20, 2004 11:44 am

Here is few lines from your code
[cpp]char inp[100];
/*freopen("c:\\inp.txt","r",stdin);*/
while(scanf("%d",&n)==1)
{
i=0;
while(scanf("%s",&inp)==1)
[/cpp]

Don' you think you made a very basic mistake here. :wink:
User avatar
shamim
A great helper
 
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Dhaka

Postby Jewel of DIU » Sat Mar 20, 2004 3:34 pm

Shamim Bahi, I didn't find any mistake in there.
What can i do?
I change my that portion of code with that. but the result is same.
[c]
char inp[100];
while(scanf("%d",&n)==1)
Hate WA
Visit phpBB!
Jewel of DIU
New poster
 
Posts: 32
Joined: Thu Jul 31, 2003 6:21 am
Location: Daffodil Univ, Bangladesh

10126 TLE

Postby medv » Sun Jun 05, 2005 8:27 pm

I have TLE in this problem because I use in a program
g += tolower(c);

in a part of code

string g;
char c;
vector<pair<string,int> > v;

while(scanf("%c",&c))
{
if (!isalpha(c))
{
if (g.size() > 0)
{
if (g == "endoftext") break;
i = Find(g);
if (i < 0) v.push_back(make_pair(g,1));
else v[i].second++;
g = "";
}
continue;
}
g += tolower(c);
}

How to increase spped?
If I use not string, but char*, then I have a question:

if I have char a[1000], where I put a word. How to convert
it into string?
medv
Learning poster
 
Posts: 85
Joined: Sun Jul 14, 2002 1:17 pm

10126 - Zipf's Law - one question

Postby Ali Arman Tamal » Sun Oct 02, 2005 10:57 am

i got rte

i think i have a problem :

does the words consists of only a-z and A-Z ? do i have to consider @ & $ etc symbol as word element or a delimiter.

Can anyone tell me the word and nonword range in this prob. :-?
User avatar
Ali Arman Tamal
Learning poster
 
Posts: 76
Joined: Sat Jan 15, 2005 5:04 pm
Location: Dhaka

Postby Larry » Sun Oct 02, 2005 11:47 am

I assume anything not in [A-Za-z] is a delimiter.
Larry
Guru
 
Posts: 647
Joined: Wed Jun 26, 2002 10:12 pm
Location: Hong Kong and New York City

Postby sakhassan » Sat Nov 25, 2006 9:10 am

I am getting TLE ??? I dont know what's wrong !!!!!!!!!!! :(

Code: Select all
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>

#define N 100
#define M 10001

struct d{

   char str[N];
   int count;

}dic[M];
int count;

void chk(char str[N])
{

   int i;
   int flag;

   flag = 0;

   for(i=0;i<count;i++)
   {

      if(strcmp(str,dic[i].str)==0)
      {
             flag = 1;
             dic[i].count++;
             break;
      }
   }

   if(!flag)
   {
      strcpy(dic[count].str,str);
      dic[count].count++;
      count++;
   }

   return ;

}


int cmp( const d *a, const d *b)
{


   return strcmp(a->str,b->str);
}


int main()
{


   int n;
   int cases;
   char str[N];
   int i;
   int idx;
   int len;
   char temp[N];
   int flag;
   char ch;


   //freopen("10126.cpp","r",stdin);

   cases = 0;
   while(scanf("%d",&n)==1)
   {

   gets(str);
   //if(strlen(str)==0)
   // continue;

   if(cases)printf("\n");

   count = 0;
   for(i=0;i<M;i++)
   {
      dic[i].count=0;
      dic[i].str[0]='\0';
   }


   while(1)
   {

      gets(str);

      if(strcmp(str,"EndOfText")==0)
       break;



      len = strlen(str);


      flag = 0;
      idx = 0;
      memset(temp,'\0',sizeof(temp));

      for(i=0;i<len;i++)
      {
         ch = str[i];
         if(!isalpha(ch))
         {
            if(flag)
            {

               temp[idx]='\0';
               chk(temp);
               idx=0;
               flag = 0;
               memset(temp,'\0',sizeof(temp));

            }
         }
         else if(isalpha(ch))
         {

             temp[idx++]=tolower(ch);
             if(!flag)flag=1;
         }
      }

      if(isalpha(ch) && flag)
      {
           temp[idx]='\0';
           chk(temp);
      }

   }

   if(count)
    qsort(dic,count,sizeof(dic[0]),(int(*)(const void *, const void *))cmp);


   for(i=0;i<count;i++)
   {
      if(dic[i].count==n)
      printf("%s\n",dic[i].str);
   }


   cases++;
   }


   return 0;
}



Time that gone is gone forever ...
sakhassan
Experienced poster
 
Posts: 105
Joined: Sat Mar 11, 2006 9:42 am
Location: cse,DU

Postby Jan » Sat Nov 25, 2006 11:48 pm

Though you are using qsort(), your complexity is actually O(n^2). Because you are listing the words first. And while listing you are checking whether this current word exists or not by checking all the other listed words. Just change this idea. First, list all the words. So, there can be duplicate words in the list. Sort them. Then you can find all the non-duplicate words and their frequency in just O(n) time. So, the total complexity will be O(n*log(n)), and which is an acceptable complexity for this problem.
Ami ekhono shopno dekhi...
HomePage
Jan
Guru
 
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh

Postby sakhassan » Sun Nov 26, 2006 5:37 pm

Thanks for ur information .... my code is lack of something ... :oops: :-?
Time that gone is gone forever ...
sakhassan
Experienced poster
 
Posts: 105
Joined: Sat Mar 11, 2006 9:42 am
Location: cse,DU

Wrong answer.....10126

Postby mirage » Wed Mar 05, 2008 8:50 am

hi friends,
i m gettin wrong answer on the OJ for this easy prob........the program is passing the test that i gave it n the one ones i found in the forums.....
still unable to find the bug....plz help......
is thr any special way to print the output for this prob...
here's the code:
Code: Select all
//Zipf's law
#include<iostream>
#include<map>
#include<string>
using namespace std;
int main(){
   int n;
   int count=0;
   while(cin>>n){
      if(count++!=0) cout<<"\n";
      string s;
      int loop=1;
      map<string,int> maps;
      while(loop==1){   
         getline(cin,s,'\n');
         s=s+" ";   
         int i=0;
         while(i<s.length()){
            string h="";
            int val=s[i];
            string alias="";
            while((val>=65 && val<=90)|| (val>=97 && val<=122)){
               if(val>=90) h=h+s[i];
               else h=h+char(s[i]+32);
               alias=alias+s[i++];
               val=s[i];
            }
            i++;
            if(alias=="EndOfText"){
               loop=0;
               break;
            }
            maps[h]=maps[h]+1;
         }
      }
      map<string,int>::iterator iter;
      iter=maps.begin();
      int l=0;
      while(iter!=maps.end()){
         if(iter->second==n) {
            cout<<iter->first<<"\n";
            l=1;
         }
         iter++;
      }
      if(l==0) cout<<"There is no such word."<<"\n";
      maps.clear();
   }
   return(0);
}
mirage
New poster
 
Posts: 11
Joined: Sat Jan 19, 2008 9:37 pm

Re: 10126 - Zipf's Law

Postby Jehad Uddin » Sat Oct 10, 2009 11:37 am

read the problems output specification,
For each test case, output the words which occur n times in the input text, one word per line, lower case, in alphabetical order
Jehad Uddin
Learning poster
 
Posts: 74
Joined: Fri May 08, 2009 5:16 pm

Re: 10126 - Zipf's Law

Postby sd12582000 » Sat May 19, 2012 1:54 pm

try
input

2
a-a
EndOfText


output
a
sd12582000
New poster
 
Posts: 1
Joined: Sat May 19, 2012 1:49 pm

Previous

Return to Volume CI

Who is online

Users browsing this forum: No registered users and 1 guest