< Back to forum

PALINGAM CODECHEF

my code is giving WA in subtask 2. some1 plzz check out the errors.

problem link :- https://www.codechef.com/AUG17/problems/PALINGAM

My Code :- 

#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long int t;
	cin>>t;
	while(t--)
	{
		char a[501],b[502];
		long long int l=strlen(a),i,cnta=0,cntb=0,r,s,count1=0,conta,contb;
		scanf("%s %s",a,b);
		long long int ha[26]={0},hb[26]={0};
		for(i=0;a[i]!='\0';i++)
		{
			r= a[i]-'a';
			s= b[i]-'a';
			ha[r]++;
			hb[s]++;
		}
		for(i=0;i<26;i++)  // counting those elements of string 'a' which are also present in string 'b' 
		{
			if(ha[i]>0)
			{
				cnta++;
				if(hb[i]>0)
				{
					cntb++;
				}
			}
		}
		for(i=0;i<26;i++)  // counting those elements of string 'b' which are also present in string 'a' 
		{
			if(hb[i]>0)
			{
				contb++;
				if(ha[i]>0)
				{
					conta++;
				}
			}
		}
		for(i=0;i<26;i++)
		{
			if(((ha[i]>=2)&&(hb[i]==0)))
			{
				cout<<"A\n";
				count1++;	
			}
		}
		if(((conta==contb)&&(cnta>cntb)))
		{
			cout<<"A\n";
			count1++;
			continue;
		}
		if(count1==0)
		cout<<"B\n";
	}
}

 

Asked by: ABHISHEK_KUMAR2 on April 7, 2019, 6:34 p.m. Last updated on April 7, 2019, 6:34 p.m.


Enter your answer details below:


Enter your comment details below:




3 Answer(s)

avatar

There are only 2 cases when A wins:

1. When it has an element more than once and it is not present in B. U have taken care of this case.

2. When A has all the elements of B and it also has some elements of its own which are not present in B. (below code)

int d=0,flag=0;

for(int i=0;i<26;i++)
{	if(ha[i]==0 && hb[i]>0)
	{	flag=2;
		break;
	}
	if(ha[i]>0 && hb[i]==0)
		d++;
}

if(flag!=2 && d>0)
{	cout<<"A"<<endl;
	continue;
}

cout<<"B"<<endl;

 

Samrat_De last updated on April 7, 2019, 6:34 p.m. 0    Reply    Upvote   

avatar

hey samrat.....thanks for your attempt....but i think that you havn't understood my code properly. The logic which you mentioned has already been implimented in the code...... but still its giving WA........ i.g there is some  other problem in the code...... so look .... if you can figure it out....  :-)

ABHISHEK_KUMAR2 last updated on April 7, 2019, 6:34 p.m. 0    Reply    Upvote   

avatar

my code got accepted..!! i used the logic that i told u....

link :  https://www.codechef.com/viewsolution/19397819

Samrat_De last updated on April 7, 2019, 6:34 p.m. 0    Reply    Upvote   

Instruction to write good question
  1. 1. Write a title that summarizes the specific problem
  2. 2. Pretend you're talking to a busy colleague
  3. 3. Spelling, grammar and punctuation are important!

Bad: C# Math Confusion
Good: Why does using float instead of int give me different results when all of my inputs are integers?
Bad: [php] session doubt
Good: How can I redirect users to different pages based on session data in PHP?
Bad: android if else problems
Good: Why does str == "value" evaluate to false when str is set to "value"?

Refer to Stack Overflow guide on asking a good question.