結果

問題 No.3109 Swap members
ユーザー MOTOHARU ONODERA
提出日時 2025-04-19 12:28:46
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 538 ms / 2,000 ms
コード長 724 bytes
コンパイル時間 2,683 ms
コンパイル使用メモリ 205,260 KB
実行使用メモリ 25,236 KB
最終ジャッジ日時 2025-04-19 12:28:58
合計ジャッジ時間 9,800 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 52
権限があれば一括ダウンロードができます

ソースコード

diff #

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


int main() {
	int n,k;
	cin >> n>>k;
	vector<string>s(n);
	vector<string>t(n);
	map<string,int>memo;
	for(int i=0; i<n;i++){
		cin >> s[i];
		memo[s[i]]=i;
	}
	
	for(int i=0; i<n;i++){
		cin >> t[i];
		
	}
	bool x=true;
	for(int i=0;i<=n-k-1;i++){
		if(s[i]==t[i]){
			
			continue;
		}
		else{
			int j=memo[t[i]];
			if((j-i)%k==0){
				while(j!=i){
					memo[s[j]]=j-k;
					memo[s[j-k]]=j;
					swap(s[j],s[j-k]);
					j-=k;
				}
			}
			else{
				x=false;
				break;
			}
		}
		
	}
	if(x==false)cout << "No" <<endl;
	else{
		x=false;
	for(int i=n-k;i<n;i++){
		if(s[i]!=t[i])break;
		else if(i==n-1)x=true;
	}
	if(x)cout << "Yes" <<endl;
	else cout << "No" <<endl;
	} 
}
0