1382A - Common Subsequence - CodeForces Solution


brute force *800

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
    n,m=map(int,input().split())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    done=False
    for e in a :
        if e in b:
            print("YES")
            print(1,e)
            done=True
            break
    if not done:
        print("NO")

C++ Code:

#include <iostream>
#include<algorithm>
#include<vector>
#include<iomanip>
using namespace std;
int main()
{
	int t, m, n;
	cin >> t;
	while (t--)
	{
		int arr[1005] = {},x,ans;
		cin >> n >> m;
		bool c = 0;
		for (int i = 0; i < n; i++)
		{
			cin >> x;
			arr[x]++;
		}
		for (int i = 0; i < m; i++)
		{
			cin >> x;
			if (arr[x])
			{
				ans = x;
				c = 1;
			}

		}
		if (c)
			cout << "YES" << endl << 1 << " " << ans << endl;
		else
			cout << "NO" << endl;

	}

	return 0;
}		


Comments

Submit
0 Comments
More Questions

1327B - Princesses and Princes
1450F - The Struggling Contestant
1399B - Gifts Fixing
1138A - Sushi for Two
982C - Cut 'em all
931A - Friends Meeting
1594A - Consecutive Sum Riddle
1466A - Bovine Dilemma
454A - Little Pony and Crystal Mine
2A - Winner
1622B - Berland Music
1139B - Chocolates
1371A - Magical Sticks
1253A - Single Push
706B - Interesting drink
1265A - Beautiful String
214A - System of Equations
287A - IQ Test
1108A - Two distinct points
1064A - Make a triangle
1245C - Constanze's Machine
1005A - Tanya and Stairways
1663F - In Every Generation
1108B - Divisors of Two Integers
1175A - From Hero to Zero
1141A - Game 23
1401B - Ternary Sequence
598A - Tricky Sum
519A - A and B and Chess
725B - Food on the Plane