QM16A - Editorial

PROBLEM LINKS :

Practice
Contest

Author : Chandan Bourah
Tester : Jaideep Pyne
Editorialist : Yash Patni

DIFFICULTY :

CAKEWALK

PREREQUISITES :

String Manipulation

PROBLEM :

For each testcase, 2 strings (sentences) are provided as input. If atleast one word is common between these 2 sentences, then print “Yes” otherwise “No”.

EXPLANATION :

The problem is pretty straightforward and self-explanatory. Since the constraints are very low, brute force method would be the best way to solve the problem.
The method of solving is to traverse both the strings, extract the words and store them in separate arrays. Then for each word in the first string, we can check the equality of this word against each word of the second string array and keep a flag. If at any point, the equality condition is satisfied we can set the flag and print “Yes” outside the loop. If the equality condition is not satisfied for any case i.e. the flag is not set, then print “No”
flag = false
for each word x in the first string :
for each word y in the second string :
if x is equal to y :
flag = true
end loop
if flag = true :
end loop
if flag = true :
print “Yes”
else
print “No”

AUTHOR’S SOLUTIONS :

using System;
using System.Collections.Generic;
class some
{
	public static void Main()
	{
		int t=int.Parse(Console.ReadLine());
		for(int i=0;i<t;i++)
		{
			string[]a=Console.ReadLine().Split();
			string[]b=Console.ReadLine().Split();
			int count=0;
			for(int j=0;j<a.Length;j++)
			{
				for(int k=0;k<b.Length;k++)
				{	
					if(a[j]==b[k])count++;
				}
			}
			if(count==0)Console.WriteLine("No");
			else Console.WriteLine("Yes");
		}
	}
}

TESTER’S SOLUTIONS :

#include <bits/stdc++.h>
#define int long long

using namespace std;

#undef int
int main() 
{
    #define int long long
    std::ios::sync_with_stdio(0);
    cin.tie(0);
    int T;
    cin >> T;
    string s;
    getline(cin, s);
    while (T--) 
    {
	set<string>ss;
	//first line
	getline(cin, s);
	string word = "";
	for(auto i : s)
	{
	    if(i == ' ')
	    {
	        ss.insert(word);
	        word = "";
	        continue;
	    }
	    word += i;
	}
	ss.insert(word);

	//second line
	int fl = 0;
	getline(cin, s);
	word = "";
	for(auto i : s)
	{
	    if(i == ' ')
	    {
	        if(ss.find(word) != ss.end())
	        {
	            fl = 1;
	            break;
	        }
	        word = "";
	        continue;
	    }
	    word += i;
	}
	if(ss.find(word) != ss.end())
	    fl = 1;
	cout << ((fl == 1) ? "Yes" : "No") << "\n";
    }
    return 0;
}