QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#785514#7882. Linguistics PuzzleThe_cosmosTL 1ms3892kbC++203.0kb2024-11-26 18:07:342024-11-26 18:07:36

Judging History

你现在查看的是最新测评结果

  • [2024-11-26 18:07:36]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3892kb
  • [2024-11-26 18:07:34]
  • 提交

answer

// Problem: I. Linguistics Puzzle
// Contest: Codeforces - The 2023 ICPC Asia Hefei Regional Contest (The 2nd Universal Cup. Stage 12: Hefei)
// URL: https://codeforces.com/gym/104857/problem/I
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>
using namespace std;
using db = double;
using lb = long double;
using ull = unsigned long long;
using uint = unsigned int;
using ll = long long;
//using i128 = __int128;
//using ui128 = unsigned __int128;
#define REP(i, first, last) for(int i = (first); i <= (last); ++ i)
#define DOW(i, first, last) for(int i = (first); i >= (last); -- i)
#define int long long
#define pb emplace_back
#define ob pop_back
#define pii pair<int, int>
#define MPR make_pair
#define fi first
#define se second
#define tpl tuple<int, int, int>
#define MTP make_tuple
#define poly vector<int>
#define polyp vector<pii>
#define polyt vector<tpl>
#define all(x) x.begin(), x.end()
#define CVR(a, q) memset(a, q, sizeof a)
#define CLR(a) memset(a, 0, sizeof a)
#define CPY(a, q) memcpy(a, q, sizeof a)
#define PCT __builtin_popcount
const int N = 100 + 5, M = 1e6 + 5;
const int mod1 = 1e9 + 7, mod2 = 998244353, mod = 1e9 + 7;
const long long inf = 1e18;
const int dx[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
const int dy[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const string st = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int n, cnt[N * N], c1[N], c2[N][N], sum[N * N], b[N], ans[N * N], pre[N];
poly w[N * N];
bool vis[N];
int get(char s) {
	if(s < 97) return s + 26 - 65;
	return s - 97;
}
void clear() {
	CLR(cnt), CLR(c1), CLR(c2), CLR(sum), CLR(b), CLR(vis), CLR(pre);
	REP(i, 0, N - 1) w[i].clear();
}
//ans 字母所映射的数字
bool check() {
	REP(i, 0, n - 1) if(c1[i] != sum[ans[i]]) return false;
	REP(i, 0, n - 1) REP(j, 0, n - 1) {
		if(!ans[i]) break;
		if(c2[i][j] != sum[ans[i] * n + ans[j]]) return false;
	}
	return true;
}
bool dfs(int u) {
	if(u == n) return check();
	for(auto x : w[cnt[u]]) {
		if(!vis[x]) {
			ans[u] = x;
			vis[x] = 1;
			if(dfs(u + 1)) return true;
			vis[x] = 0;
		}
	}
	return false;
}
void Solve() {
	clear();
	cin >> n;
	REP(i, 0, n * n - 1) {
		string s; cin >> s;
		if(s.size() == 1) {
			c1[get(s[0])] ++;
			cnt[get(s[0])] ++;
		}
		else {
			c2[get(s[0])][get(s[1])] ++;
			cnt[get(s[0])] ++, cnt[get(s[1])] ++;
		}
	}
	REP(i, 0, n - 1) REP(j, 0, n - 1) {
		sum[i * j] ++;
		if(i * j >= n) b[i * j / n] ++;
		b[i * j % n] ++;
	}
	REP(i, 0, n - 1) w[b[i]].pb(i);
	dfs(0);
	REP(i, 0, n - 1) pre[ans[i]] = i;
	REP(i, 0, n - 1) cout << st[pre[i]];
	cout << '\n';
}
signed main() {
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
	int T = 1;
	cin >> T;
	while (T --) {
		Solve();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3892kb

input:

2
3
a b a b b b b c cc
4
d d d d d c b a d b cd cb d a cb bc

output:

bca
dcba

result:

ok OK

Test #2:

score: 0
Accepted
time: 1ms
memory: 3868kb

input:

2
4
d a a bc ba bc b a a a d a a cb c c
4
a b da b b d ad b db b a c da b c b

output:

abcd
bdac

result:

ok OK

Test #3:

score: -100
Time Limit Exceeded

input:

50
3
b b b a a c b b cc
4
d ab c ad d b ba ab c b d d d d d a
5
a aa aa ab ab ae b b e c c c ba c c c c dd d d dd c e c e
6
a ca a a a a a a ce a a b ba ba bc bc bd be e c c ca a cd cd be d d dc dc e e a eb f f
7
a a a a a a a a cf a a a a b b b b c c c cf a dd d dc d dd e f ed ee ee fb eg eg eg eg ...

output:


result: