0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-23 18:48:48 +00:00

P1092 [NOIP2004 提高组] 虫食算

https://www.luogu.com.cn/record/85702831
This commit is contained in:
Baoshuo Ren 2022-09-02 19:48:01 +08:00
parent 2d161dd033
commit 5c7ea9cfd8
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

115
Luogu/P1092/P1092.cpp Normal file
View File

@ -0,0 +1,115 @@
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 30;
int n;
bool vis[N];
std::string s1, s2, s3;
std::map<char, int> map;
std::vector<char> chs;
bool check() {
for (int i = n - 1; i >= 0; i--) {
if (map[s1[i]] != -1 && map[s2[i]] != -1 && map[s3[i]] != -1) {
if ((map[s1[i]] + map[s2[i]]) % n != map[s3[i]]
&& (map[s1[i]] + map[s2[i]] + 1) % n != map[s3[i]]) {
return false;
}
} else if (map[s1[i]] != -1 && map[s2[i]] != -1) { // map[s3[i]] == -1
if (vis[(map[s1[i]] + map[s2[i]]) % n]
&& vis[(map[s1[i]] + map[s2[i]] + 1) % n]) {
return false;
}
} else if (map[s1[i]] != -1 && map[s3[i]] != -1) { // map[s2[i]] == -1
if (vis[(map[s3[i]] - map[s1[i]] + n) % n]
&& vis[(map[s3[i]] - map[s1[i]] - 1 + n) % n]) {
return false;
}
} else if (map[s2[i]] != -1 && map[s3[i]] != -1) { // map[s1[i]] == -1
if (vis[(map[s3[i]] - map[s2[i]] + n) % n]
&& vis[(map[s3[i]] - map[s2[i]] - 1 + n) % n]) {
return false;
}
}
}
return true;
}
bool check_ans() {
int x = 0;
for (int i = n - 1; i >= 0; i--) {
if ((x + map[s1[i]] + map[s2[i]]) % n != map[s3[i]]) {
return false;
}
x = (x + map[s1[i]] + map[s2[i]]) / n;
}
return x == 0;
}
void dfs(int x) {
if (x == n) {
if (check_ans()) {
for (auto o : map) {
cout << o.second << ' ';
}
cout << endl;
exit(0);
}
return;
}
for (int i = n - 1; i >= 0; i--) {
if (vis[i]) continue;
vis[i] = true;
map[chs[x]] = i;
if (check()) dfs(x + 1);
map[chs[x]] = -1;
vis[i] = false;
}
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> s1 >> s2 >> s3;
for (int i = n - 1; i >= 0; i--) {
if (!map.count(s1[i])) {
map[s1[i]] = -1;
chs.emplace_back(s1[i]);
}
if (!map.count(s2[i])) {
map[s2[i]] = -1;
chs.emplace_back(s2[i]);
}
if (!map.count(s3[i])) {
map[s3[i]] = -1;
chs.emplace_back(s3[i]);
}
}
dfs(0);
return 0;
}