10340 - All in All

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

Moderator: Board moderators

ximopl
New poster
Posts: 2
Joined: Sat Jan 11, 2003 12:13 am
Location: Valencia, Spain

10340

Post by ximopl »

I don't know why always get WA. I can't find any mistake in the code. I hope somebody could find it.
thanks

Code: Select all

#include <stdio.h>
#define SIZE 1000000

int main()
{
	char cad1[SIZE], cad2[SIZE];
	int x,y;
	
	while ( scanf("%s %s",cad1,cad2) == 2 )
	{
		for (x = 0, y = 0; cad1[x] != '\0' && cad2[y] != '\0'; x++, y++)
		{
			if (cad1[x] != cad2[y])
				for ( ; cad2[y] != cad1[x] && cad2[y] != '\0'; y++);
		}
		
		if (cad1[x] != '\0') printf("No\n");
		else printf("Yes\n");
	}
	return 0;
}

Jucovschi Constantin
New poster
Posts: 9
Joined: Sat Oct 26, 2002 6:30 pm

Post by Jucovschi Constantin »

Try this test

Code: Select all

sea esa
Your program says "YES"

raysa
New poster
Posts: 40
Joined: Wed Dec 18, 2002 4:23 pm
Location: Indonesia

TLE

Post by raysa »

And I'm getting TLE.. Any idea?

[cpp]
#include <stdio.h>
#include <string.h>

void main ()
{
char word1[100000],word2[100000];
long int i,j,begin,result,code;

while (scanf ("%s %s",word1,word2)==2)
{
result=0; begin=0; code=0;
if (strlen (word1)<=strlen (word2))
{
for (i=0;i<=strlen(word1)-1;i++)
{
if (begin<strlen(word2)-1)
{
for (j=begin;j<=strlen (word2)-1;j++)
{
if (code==strlen(word1)) {break;}
else if (word1==word2[j]) {begin=j; code++; break;}
else if (j==strlen (word2)-1) {result=1;}
}
if (i<strlen(word1)-1 && begin==strlen(word2)-1) {result=1; break;}
}
if (code==strlen(word1) || result==1) break;
if (i==strlen(word1)-1) {result=0;}
}

if (result==0) {printf ("Yes\n");}
else if (result==1) {printf ("No\n");}
}
else {printf ("No\n");}
}
}[/cpp]

ximopl
New poster
Posts: 2
Joined: Sat Jan 11, 2003 12:13 am
Location: Valencia, Spain

Post by ximopl »

you do a lot of strlen() that spent a lot of time.
try to calcule the length at the start and save it in a var.

deddy one
Experienced poster
Posts: 120
Joined: Tue Nov 12, 2002 7:36 pm

Post by deddy one »

also you'll only need 2 "loop" (nested of course)
with one "if" for checking the letter
and store that letter and than
compare it with the input string(in the end).

good luck.

raysa
New poster
Posts: 40
Joined: Wed Dec 18, 2002 4:23 pm
Location: Indonesia

Post by raysa »

Thank's a lot, ximopl!
I've never consider that strlen() would cause a terrible time
consumption...

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

10340 all in all, why run time error

Post by sohel »

---
Last edited by sohel on Thu Feb 02, 2006 4:27 pm, edited 1 time in total.

Larry
Guru
Posts: 647
Joined: Wed Jun 26, 2002 10:12 pm
Location: Hong Kong and New York City
Contact:

Post by Larry »

Try a bigger array.. =)

Red Scorpion
Experienced poster
Posts: 192
Joined: Sat Nov 30, 2002 5:14 am

10340

Post by Red Scorpion »

Ah, This is a frustating Problem, I have try to submitted it several times, but results WA.

Please help me,
This is my code :

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

char s[MAX], t[MAX], ls, lt;

int exist(char q, int *index) {
int i;

for (i=0; i<lt; i++) {
if (i>*index && t[i] == q) {
t[i] = 0;
*index = i;
return (1);
}
}
return (0);
}

void process() {
int i,index=-1;

for (i=0; i<ls; i++) {
if (!exist(s[i], &index)) {
printf ("No\n");
return;
}
}
printf ("Yes\n");
}

int main() {
char temp[MAX];
while (scanf ("%s %s", s, t)==2) {
ls = strlen(s); lt = strlen(t);
if (ls > lt) printf ("No\n");
else process();
}
return (0);
}
:lol:

Thanks,
Red Scorpion[list][/list]

Red Scorpion
Experienced poster
Posts: 192
Joined: Sat Nov 30, 2002 5:14 am

Post by Red Scorpion »

Finally I GOT AC.
THANKS :lol:

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

10340 AllinAll help!!!

Post by sohel »

I don't understand why this program is not accepted.
I get wrong answer.



here is my code:

[cpp]

#include<stdio.h>
#include<string.h>

int main()
{
int i,j=0,cnt=0,lf,ls;
char frst[1000001];
char scnd[1000001];
char ch;
while(scanf("%s",frst)!=EOF)
{

lf=strlen(frst);
scanf("%s",scnd);
ls=strlen(scnd);
for(i=0;i<lf;i++)
{
while(1)
{
if(frst[i]==scnd[j])
{ cnt++;j++;break;}
j++;
}
if(j>=ls) break;
}

if(cnt==(lf))
printf("Yes\n");
else printf("No\n");
cnt=0;
j=0;
}
return 0;
}[/cpp]
Last edited by sohel on Thu Mar 04, 2004 12:02 pm, edited 1 time in total.

Adil
Learning poster
Posts: 57
Joined: Sun Sep 29, 2002 12:00 pm
Location: in front of the monitor :-)
Contact:

Post by Adil »

hello.

this part of your code seems to be a little off the track.

Code: Select all

 			while(1)
 			{
 				if(frst[i]==scnd[j])
 				{	cnt++;j++;break;}
 				j++;
 			}
the loop should continue only until the end of the second string has been reached.

hope this helps.

another thing: declaring large size arrays (in this case, your input strings) inside a function is not a good idea. try declaring large-arrays outside any function, ie, try to make them global.

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

Still confused!@#

Post by sohel »

oops. :o
Last edited by sohel on Tue Jul 27, 2004 3:04 pm, edited 1 time in total.

Hisoka
Experienced poster
Posts: 120
Joined: Wed Mar 05, 2003 10:40 am
Location: Indonesia

Post by Hisoka »

maybe Adil means your problem like this:

Code: Select all

for(i=0;i<lf;i++) 
{ 
while(1) 
{ 
if(frst[i]==scnd[j]) 
{ cnt++;j++;break;} 
j++; 
} 
if(j>=ls) break; 
} 
when (j>=ls&&frst!=scnd[j]) you repeat again the proses, or when you break from the loop you get it wrong answer. and this is looping forever.
change your program with this:

Code: Select all

for(i=0;i<lf;i++) 
{ 
while(1) 
{ 
if(frst[i]==scnd[j]) 
{ cnt++;j++;break;} 
j++; 
if(j>=ls) break;
} 
}
:wink:

coolbila
New poster
Posts: 8
Joined: Tue Apr 01, 2003 8:11 pm

10340 TLE ????? i can't believe it

Post by coolbila »

#include <stdio.h>
#include <string.h>

int main()
{
char str1[100000],str2[100000];
int top;
int i;
while(scanf("%s %s",str1,str2)!=EOF)
{
top=0;
for(i=0;i<=strlen(str2)-1;i++)
{
:evil: if(str1[top]==str2)
top++;
}
if(top>=strlen(str1)-1)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
Oh my God ... Wrong Answer

Post Reply

Return to “Volume 103 (10300-10399)”