結果

問題 No.470 Inverse S+T Problem
ユーザー FF256grhyFF256grhy
提出日時 2016-12-23 20:19:08
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 918 ms / 2,000 ms
コード長 3,051 bytes
コンパイル時間 1,416 ms
コンパイル使用メモリ 156,728 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-24 01:25:34
合計ジャッジ時間 5,718 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 376 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 5 ms
4,376 KB
testcase_15 AC 19 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 24 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 579 ms
4,376 KB
testcase_26 AC 918 ms
4,380 KB
testcase_27 AC 646 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long   signed int LL;
typedef long long unsigned int LU;

#define incID(i, l, r) for(int i = (l)    ; i <  (r); i++)
#define incII(i, l, r) for(int i = (l)    ; i <= (r); i++)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--)
#define decII(i, l, r) for(int i = (r)    ; i >= (l); i--)
#define inc( i, n) incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec( i, n) decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)

#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))

#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define UB upper_bound
#define LB lower_bound
#define PQ priority_queue

#define  ALL(v)  v.begin(),  v.end()
#define RALL(v) v.rbegin(), v.rend()
#define  FOR(i, v) for(auto i =  v.begin(); i !=  v.end(); ++i)
#define RFOR(i, v) for(auto i = v.rbegin(); i != v.rend(); ++i)

template<typename T> bool setmin(T & a, T b) { if(a <= b) { return false; } else { a = b; return true; } }
template<typename T> bool setmax(T & a, T b) { if(b <= a) { return false; } else { a = b; return true; } }
template<typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

template<typename T> ostream & operator<<(ostream & os, const vector<T> & v) {
	os << "[";
	FOR(it, v) {
		if(it != v.begin()) { os << ", "; }
		os << *it;
	}
	os << "]";
	return os;
}
template<typename F, typename S> ostream & operator<<(ostream & os, const pair<F, S> & p) {
	os << "<" << p.FI << ", " << p.SE << ">";
	return os;
}

// ---- ----

int n;
string u[52];
set<string> ss;

bool f[52];
LL counter;
bool search(int i) {
	if(i == n) { return true; }
	
	if(counter++ > 1000000) { return false; } // ひどい
	
	string sa = u[i].substr(0, 1);
	string sb = u[i].substr(1);
	string sc = u[i].substr(0, 2);
	string sd = u[i].substr(2);
	
	bool fab = (ss.count(sa) == 0 && ss.count(sb) == 0);
	bool fcd = (ss.count(sc) == 0 && ss.count(sd) == 0);
	
	if(fab) {
		incID(j, i + 1, n) {
			if(
				(sa == u[j].substr(0, 1) && sa == u[j].substr(2)) ||
				(sb == u[j].substr(0, 2) && sb == u[j].substr(1))
			) { fab = false; break; }
		}
	}
	if(fab) {
		ss.insert(sa);
		ss.insert(sb);
		f[i] = true;
		if(search(i + 1)) { return true; }
		ss.erase(sa);
		ss.erase(sb);
	}
	
	if(fcd) {
		incID(j, i + 1, n) {
			if(
				(sc == u[j].substr(0, 1) && sc == u[j].substr(2)) ||
				(sd == u[j].substr(0, 2) && sd == u[j].substr(1))
			) { fcd = false; break; }
		}
	}
	if(fcd) {
		ss.insert(sc);
		ss.insert(sd);
		f[i] = false;
		if(search(i + 1)) { return true; }
		ss.erase(sc);
		ss.erase(sd);
	}
	
	return false;
}

bool solve() {
	if(n > 52) { return false; }
	inc(i, n) { cin >> u[i]; }
	
	return search(0);
}

int main() {
	cin >> n;
	
	if(! solve() ) {
		cout << "Impossible" << endl;
	} else {
		inc(i, n) {
			cout << u[i].substr(0, (f[i] ? 1 : 2)) << " " << u[i].substr(f[i] ? 1 : 2) << endl;
		}
	}
	
	return 0;
}
0