Submission #1293307
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
#define _p(...) (void)printf(__VA_ARGS__)
#define forr(x,arr) for(auto&& x:arr)
#define _overload3(_1,_2,_3,name,...) name
#define _rep2(i,n) _rep3(i,0,n)
#define _rep3(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,_rep3,_rep2,)(__VA_ARGS__)
#define _rrep2(i,n) _rrep3(i,0,n)
#define _rrep3(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define rrep(...) _overload3(__VA_ARGS__,_rrep3,_rrep2,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define bit(n) (1LL<<(n))
#define sz(x) ((int)(x).size())
#define ten(n) ((int)1e##n)
#define fst first
#define snd second
using ll=long long;
using pii=pair<int,int>;using pll=pair<ll,ll>;using pil=pair<int,ll>;using pli=pair<ll,int>;
using vs=vector<string>;using vvs=vector<vs>;using vvvs=vector<vvs>;
using vb=vector<bool>;using vvb=vector<vb>;using vvvb=vector<vvb>;
using vi=vector<int>;using vvi=vector<vi>;using vvvi=vector<vvi>;
using vl=vector<ll>;using vvl=vector<vl>;using vvvl=vector<vvl>;
using vd=vector<double>;using vvd=vector<vd>;using vvvd=vector<vvd>;
using vpii=vector<pii>;using vvpii=vector<vpii>;using vvvpii=vector<vvpii>;
template<class T> bool amax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> bool amin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
ll ri(){ll l;cin>>l;return l;} string rs(){string s;cin>>s;return s;}
template<class T>T read(){T t;cin>>t;return t;}
template<class T,class U>ostream&operator<<(ostream&o,const pair<T,U>&p){return o<<'('<<p.fst<<", "<<p.snd<<')';}
ostream&operator<<(ostream&o,const vb&t){forr(e,t)o<<"#."[e];return o;}
template<class T>ostream&operator<<(ostream&o,const vector<T>&t){o<<"{";forr(e,t)o<<e<<",";o<<"}"<<endl;return o;}
#ifdef LOCAL
vs s_p_l_i_t(const string&s,char c){vs v;stringstream ss(s);string x;while(getline(ss,x,c))v.emplace_back(x);return move(v);}
void e_r_r(vs::iterator it){cerr<<endl;}
template<class T,class... Args>void e_r_r(vs::iterator it,T a,Args... args){cerr<<it->substr((*it)[0]==' ',it->length())<<" = "<<a<<", ";e_r_r(++it,args...);}
#define out(args...){vs a_r_g_s=s_p_l_i_t(#args,',');e_r_r(a_r_g_s.begin(),args);}
#else
#define out(args...)
#endif
struct UnionFind {
const int V; // 頂点数
vector<int> par, rank, cnt; // 親の番号, 木の大きさ, 同じ親の頂点数
UnionFind(int V) : V(V), par(V), rank(V), cnt(V) { init(); }
// 初期化する
void init() {
for (int i = 0; i < V; i++) par[i] = i;
fill(rank.begin(), rank.end(), 0);
fill(cnt.begin(), cnt.end(), 1);
}
// 木の根を求める
int find(int x) {
return par[x] == x ? x : par[x] = find(par[x]);
}
// x と y の属する集合を併合
// 併合前は違う集合だったら true を返す
bool unite(int x, int y) {
if ((x = find(x)) == (y = find(y))) return false;
cnt[y] = cnt[x] = cnt[x] + cnt[y];
if (rank[x] < rank[y]) par[x] = y;
else {
par[y] = x;
if (rank[x] == rank[y]) rank[x]++;
}
return true;
}
// x と y が同じ集合に属するか否か
bool same(int x, int y) {
return find(x) == find(y);
}
// x と同じ集合にいる頂点数を返す
int count(int x) {
return cnt[find(x)];
}
};
ll solve() {
int n = ri();
auto S = rs();
auto T = rs();
UnionFind uf(26);
vi C(26, -1);
vi A;
rep(i, n) {
char s = S[i];
char t = T[i];
if ('A' <= s && s <= 'Z') A.emplace_back(s-'A');
if ('A' <= t && t <= 'Z') A.emplace_back(t-'A');
if ('A' <= s && s <= 'Z') {
if ('A' <= t && t <= 'Z') {
uf.unite(s-'A', t-'A');
}
else {
C[s-'A'] = t - '0';
}
}
else {
if ('A' <= t && t <= 'Z') {
C[t-'A'] = s - '0';
}
else {
if (s != t) {
return 0;
}
}
}
}
sort(all(A));
A.erase(unique(A.begin(), A.end()), A.end());
vi G;
forr(i, A) {
int g = uf.find(i);
G.emplace_back(g);
if (C[i] != -1) {
if (C[g] != -1 && C[g] != C[i]) return 0;
C[g] = C[i];
}
}
sort(all(G));
G.erase(unique(G.begin(), G.end()), G.end());
out(C);
int gs0 = -1;
int gt0 = -1;
if ('A' <= S[0] && S[0] <= 'Z') {
gs0 = uf.find(S[0] - 'A');
}
if ('A' <= T[0] && T[0] <= 'Z') {
gt0 = uf.find(T[0] - 'A');
}
ll ans = 1;
forr(g, G) {
if (C[g] == -1) {
if (g == gs0 || g == gt0) ans *= 9;
else ans *= 10;
}
}
return ans;
}
void Main() {
cout << solve() << endl;
}
int main() { cin.tie(nullptr); ios::sync_with_stdio(false); Main(); return 0; }
Submission Info
Submission Time |
|
Task |
B - メタ構文変数 |
User |
shiratty8 |
Language |
C++14 (GCC 5.4.1) |
Score |
0 |
Code Size |
4784 Byte |
Status |
WA |
Exec Time |
1 ms |
Memory |
256 KB |
Judge Result
Set Name |
Sample |
Subtask1 |
Subtask2 |
Subtask3 |
Score / Max Score |
0 / 0 |
0 / 40 |
0 / 30 |
0 / 30 |
Status |
|
|
|
|
Set Name |
Test Cases |
Sample |
sample_01.txt, sample_02.txt |
Subtask1 |
sample_01.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt |
Subtask2 |
sample_01.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask2_01.txt, subtask2_02.txt, subtask2_03.txt, subtask2_04.txt, subtask2_05.txt, subtask2_06.txt, subtask2_07.txt, subtask2_08.txt |
Subtask3 |
sample_01.txt, sample_02.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask2_01.txt, subtask2_02.txt, subtask2_03.txt, subtask2_04.txt, subtask2_05.txt, subtask2_06.txt, subtask2_07.txt, subtask2_08.txt, subtask3_01.txt, subtask3_02.txt, subtask3_03.txt, subtask3_04.txt, subtask3_05.txt, subtask3_06.txt, subtask3_07.txt, subtask3_08.txt |
Case Name |
Status |
Exec Time |
Memory |
sample_01.txt |
WA |
1 ms |
256 KB |
sample_02.txt |
WA |
1 ms |
256 KB |
subtask1_01.txt |
WA |
1 ms |
256 KB |
subtask1_02.txt |
WA |
1 ms |
256 KB |
subtask1_03.txt |
AC |
1 ms |
256 KB |
subtask1_04.txt |
WA |
1 ms |
256 KB |
subtask1_05.txt |
WA |
1 ms |
256 KB |
subtask1_06.txt |
WA |
1 ms |
256 KB |
subtask1_07.txt |
AC |
1 ms |
256 KB |
subtask1_08.txt |
AC |
1 ms |
256 KB |
subtask1_09.txt |
WA |
1 ms |
256 KB |
subtask1_10.txt |
WA |
1 ms |
256 KB |
subtask2_01.txt |
WA |
1 ms |
256 KB |
subtask2_02.txt |
WA |
1 ms |
256 KB |
subtask2_03.txt |
WA |
1 ms |
256 KB |
subtask2_04.txt |
WA |
1 ms |
256 KB |
subtask2_05.txt |
AC |
1 ms |
256 KB |
subtask2_06.txt |
AC |
1 ms |
256 KB |
subtask2_07.txt |
WA |
1 ms |
256 KB |
subtask2_08.txt |
WA |
1 ms |
256 KB |
subtask3_01.txt |
WA |
1 ms |
256 KB |
subtask3_02.txt |
WA |
1 ms |
256 KB |
subtask3_03.txt |
WA |
1 ms |
256 KB |
subtask3_04.txt |
WA |
1 ms |
256 KB |
subtask3_05.txt |
AC |
1 ms |
256 KB |
subtask3_06.txt |
AC |
1 ms |
256 KB |
subtask3_07.txt |
WA |
1 ms |
256 KB |
subtask3_08.txt |
WA |
1 ms |
256 KB |