B - Same Name Editorial by en_translator


Since the constraint of \(N\) is as small as \(1000\), we may check for every pair of integers \((i,j)\) such that \(1 \leq i \lt j \leq N\) if it satisfies the condition described in the program, that is, check if both \(S_i=S_j\) and \(T_i=T_j\) holds.

The complexity is \(O(N(X+Y))\), where \(X\) is the sum of lengths of \(S_i\) and \(Y\) is the sum of lengths of \(T_i\).

Sample code (C++)

#include<bits/stdc++.h>
using namespace std;

int main(){
	int N; cin >> N;
	vector<string> S(N),T(N);
	string ans = "No";
	for(int i=0; i<N; i++){
		cin >> S[i] >> T[i];
		for(int j=0; j<i; j++){
			if(S[i]==S[j] && T[i]==T[j]) ans = "Yes";
		}
	}
	cout << ans << endl;
}

Sample code (Python)

N = int(input())
S = ['']*N
T = ['']*N
for i in range(N):
    S[i],T[i] = map(str,input().split())
ans = 'No'
for i in range(N):
    for j in range(i+1,N):
        if S[i]==S[j] and T[i]==T[j]:
            ans = 'Yes'
print(ans)

posted:
last update: