10293 - Word Length and Frequency

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

Moderator: Board moderators

FT
New poster
Posts: 2
Joined: Tue Jun 25, 2002 8:21 am

10293

Post by FT » Sat Jul 06, 2002 3:38 pm

My program got Time Limit Exceeded.
Any one can help me.
Correct my mistake please.
Thank you.

Code: Select all

[cpp]

#include<iostream.h>
#include<math.h>
#include<stdio.h>
#define in cin

int len[31];
void main()
{
	char ch;
	int ltemp;
                int i,flag;
	while(in.get(ch)){
	  for(i=0;i<31;i++) len[i]=0;
	  ltemp=0;
	  flag=0;

	  while(ch!='#'){
		  if(ch==' '||ch=='?'||ch=='!'||ch==','||ch=='.'){
		    len[ltemp]++;
		    if(flag) flag=0;
		    ltemp=0;
		  }
		  if(ch=='-'){
		    flag=1;
		  }
		  if(ch=='\n'&&!flag){
		    len[ltemp]++;
		    if(flag) flag=0;
		    ltemp=0;
		  }
		  if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')){
		     ltemp++;
		  }
	                     in.get(ch);
	  }
	  for(i=1;i<31;i++)
                     if(len[i]) cout<<i<<" "<<len[i]<<endl;
	  cout<<endl;
	  in.get(ch);
	  if(ch==EOF) break;
	}
}
[/cpp]

Picard
Learning poster
Posts: 96
Joined: Mon Jun 24, 2002 1:22 pm
Location: Hungary
Contact:

Post by Picard » Sat Jul 06, 2002 4:24 pm

your inner cycle stops only when ch=='#' but not for ch==EOF which can occur for example when the last '#' is followed by a newline.

Sajid
Learning poster
Posts: 94
Joined: Sat Oct 05, 2002 5:34 pm
Location: CS - AIUB, Dhaka, Bangladesh.
Contact:

TLE

Post by Sajid » Sat Nov 01, 2003 8:34 pm

i checked both for '#' and EOF.. but why TLE ?
Sajid Online: www.sajidonline.com

Joseph Kurniawan
Experienced poster
Posts: 136
Joined: Tue Apr 01, 2003 6:59 am
Location: Jakarta, Indonesia

Post by Joseph Kurniawan » Sat Nov 08, 2003 4:21 pm

Well, you can try scanf() and printf() instead of cin and cout since these two I/O handler of C++ works quite slow compared to C I/O handler.
Hope it helps!! :wink: :wink:
There are 3 things one need to be successful : motivation, ability and chance. And if you're a believer, make it four : God's will.

Sajid
Learning poster
Posts: 94
Joined: Sat Oct 05, 2002 5:34 pm
Location: CS - AIUB, Dhaka, Bangladesh.
Contact:

Post by Sajid » Sat Nov 08, 2003 4:27 pm

I dont use cin or cout, :-?
since I am comfortable with C
.
Sajid Online: www.sajidonline.com

User avatar
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Post by sohel » Mon Dec 01, 2003 3:40 pm

I got TLE too.
But later by trial and error I managed to figure out that there are some extra new lines at the end of the last block.

:-?

User avatar
tuman
New poster
Posts: 24
Joined: Sat Oct 22, 2005 7:30 pm
Location: CUET
Contact:

10293 w.a(Word Length and Frequency)critical i\o needed

Post by tuman » Mon May 01, 2006 8:48 pm

hi guys, i m getting wrong answer :roll: in this problem for a long time. Plz provide some input and output so that i can justify my code.

Thanks in advance......and i m waiting for someones reply.

jan_holmes
Experienced poster
Posts: 136
Joined: Fri Apr 15, 2005 3:47 pm
Location: Singapore
Contact:

I/O

Post by jan_holmes » Wed May 03, 2006 7:24 pm

Try this Input :

hyper!..Goal,terM,cham-
pion. Try it A!g!a!iA.N
Thank-
s
you.
#

The output should be :
1 4
2 2
3 2
4 2
5 1
6 1
8 1

unaben
New poster
Posts: 12
Joined: Mon Jul 10, 2006 10:47 pm

10293 - Word Length and Frequency

Post by unaben » Mon Jul 10, 2006 10:53 pm

I got run time error on this problem. :cry: Could someone please help me. :o
Here is my code:

Removed
Last edited by unaben on Mon Jul 10, 2006 11:21 pm, edited 2 times in total.

unaben
New poster
Posts: 12
Joined: Mon Jul 10, 2006 10:47 pm

Post by unaben » Mon Jul 10, 2006 11:02 pm

I got AC now :D

User avatar
tuman
New poster
Posts: 24
Joined: Sat Oct 22, 2005 7:30 pm
Location: CUET
Contact:

Post by tuman » Tue Oct 17, 2006 3:58 am

After a long time, i ve found this post is replied and i got it.

Thanx i got it............AC :-?
We the dreamer of the dreamy dream...

yiuyuho
A great helper
Posts: 325
Joined: Thu Feb 21, 2002 2:00 am
Location: United States
Contact:

Post by yiuyuho » Sun Jan 14, 2007 8:22 pm

Well, I am getting P.E. now, may be that's the reason (I ended up reading the extra lines as a new input case)!!

nymo
Experienced poster
Posts: 149
Joined: Sun Jun 01, 2003 8:58 am
Location: :)

Newlines after the last case!!!

Post by nymo » Wed Feb 28, 2007 6:29 pm

Hi, I also got P.E. and after reading this post, I changed my code to handle newlines after the last case... it is ACC now. If you got P.E., this change is worth trying. Thanks to Sohel for pointing out this.

mohsincsedu
Learning poster
Posts: 63
Joined: Tue Sep 20, 2005 12:31 am
Location: Dhaka
Contact:

Post by mohsincsedu » Sun Apr 01, 2007 7:14 pm

Need more IO...


I Got PE .....


Thanks in advance
Amra korbo joy akhdin............................

User avatar
algoJo
New poster
Posts: 37
Joined: Sun Dec 17, 2006 9:02 am

Post by algoJo » Sun Apr 22, 2007 8:03 am

Hi, I got RE for this problem...
any hints??
Thanks.. :D

Code: Select all

#include<stdio.h>
#include<string.h>
#define MAX 100

char arr[10000][MAX];
int res[50];

void compute(int L){
	int i,j,len,flag=0;
	int temp=0;
	for(i=0;i<L;i++)
	{
		len=strlen(arr[i]);
		if(!flag)
		{
			res[temp]++;temp=0;
		}
		for(j=0;j<len;j++)
		{
				if(arr[i][j]=='-'||arr[i][j]=='\'') flag=1;
				else if(arr[i][j]!=' '&&arr[i][j]!='?'&&arr[i][j]!='!'&&arr[i][j]!=','&&arr[i][j]!='.') {temp++;flag=0;}
				else
				{

					flag=0;
					res[temp]++;temp=0;
				}
				if(j+1>=len&&!flag)
				{
					flag=0;
					res[temp]++;temp=0;
				}
		}
	}
	for(i=1;i<50;i++)
		if(res[i]) printf("%d %d\n",i,res[i]);
}


void main(){
	char line[MAX];
	int I=1;
	while(gets(line))
	{
		memset(arr,0,sizeof(arr));
		I=1;
		strcpy(arr[0],line);
		while(1)
		{
			gets(line);
			if(!strcmp(line,"#")) break;
			strcpy(arr[I],line);
			I++;
		}
		compute(I);
		printf("\n");
		memset(res,0,sizeof(res));
		memset(arr,0,sizeof(arr));
	}
}

Post Reply

Return to “Volume 102 (10200-10299)”