Chef and Sequence giving wrong answer (CHEFSQ)

Here’s my code i dont know in which case its failing .

#include<iostream>
#include<cstdio>
#define FAST_IO ios_base::sync_with_stdio(false);cin.tie(NULL);

using namespace std;

int main()
{
FAST_IO
int T,n,f_n;
cin>>T;
while(T--)
{
int f_count=0;
	cin>>n;
	int orig_a[n];
	
	for(int i=0;i<n;i++)
		cin>>orig_a[i];
	
	cin>>f_n;
	int f_a[f_n];
	
	for(int i=0;i<f_n;i++)
		cin>>f_a[i];

	for(int i=0;i<n;i++)
	{
		f_count=0;
		if(f_a[0]==orig_a[i])
		{
			int temp=i+1;
			f_count++;
			for(int j=1;j<f_n;j++)
			{
				if(f_a[j]==orig_a[temp++])
					f_count++;
				else
					break;
			}
			
				
		}
		if(f_count==f_n)
				break;
	}
	
	if(f_count==f_n)
		cout<<"Yes"<<"\n";
	else
		cout<<"No"<<"\n";



}

return 0;
}

On my machine, your program gives the answer Yes for the testcase:

1                   
5
1 2 3 5 5
4
1 3 4 5

although there’s an element of unpredictability here as you are mixing C-style input with iostream-based input while deliberately putting them out of sync.

Edit:

To be explicit, either:

i) Use scanf exclusively, and not cin;
ii ) use cin exclusively, and not scanf; or
iii) Remove the call to FAST_IO.

1 Like

Now ,i have changed the code and for your test case iam getting No in my machine .but its still giving wrong answer.

Ugh - it’s an unbelievably sloppily-written question, but looking at some of the AC submissions, it appears that the elements of F need not be consecutive in N i.e. we need to check whether F is a subsequence of N, in spite of the question asking to check whether it is a substring. (sigh).

For example, a randomly-chosen AC solution gives Yes for the testcase:

1                   
8
1 2 3 8 8 5 6 5
4
1 3 5 5

(

1 2 3 8 8 5 6 5
^   ^     ^   ^

)