Problem:
K - encode/decode
以下の２条件を全て満たす文字列Tの事を「良い」文字列と呼ぶことにする
TはABCDEFGHIの9種類の文字列からなる
T上の連続する２文字は次のグラフ上でも隣接している
「良い」文字列の例
BEB
ABCDCBEFGHI
「良い」文字列ではない例
AC(AとCは隣接していない)
AABCD(同じ文字はグラフ上では隣接していないとみなす)
以下の条件を満たすような２つの関数,encodeとdecodeを実装せよ.
任意の01列Sに対し,encode(S)は「良い」文字列である
任意の01列Sに対し,decode(encode(S))=Sを満たす
入出力形式
この問題にはencodeフェーズとdecodeフェーズがあり, それぞれ独立にプログラムが実行される.
encodeフェーズのとき, 入力は以下の形式で与えられる.
encode
N
S
N
は与えられる01列
S
の長さである.
S
はあなたがencodeするべき01列である.
この時の,あなたが提出するプログラムの出力を
T
とする.
T
が良い文字列でない場合は不正解となり,decodeフェーズは実行されない.
decodeフェーズのとき,入力は以下の形式で与えられる.
decode
M
T
T
はencodeフェーズにおけるあなたの出力である.
M
は文字列
T
の長さである.
この時の,あなたが提出するプログラムの出力がencodeフェーズにおけるSと一致する場合,正解となる
採点方式
この問題では入力
S
に対する出力encode(S)の長さに応じて得点が決まる.
全てのテストケースについて, |encode(S)| ≦ |S| + 10 を満たすときこの問題に対する得点が満点となる.
また, |encode(S)| ≦ 2×|S| + 10 を満たす場合, この問題に対する得点は50点となる.
制約
1 ≤ N ≤ 300000
入出力例
encodeフェーズ
入力
encode
6
001100
出力
ABCBE
encodeフェーズの終了後あなたのプログラムは一旦終了し,次にdecodeフェーズが開始する
decodeフェーズ
入力
decode
5
ABCBE
出力
001100

Submitted solutions:

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

int main() {
  map<string, pair<string, string>> edges = {
    {"A1", {"B1", "B2"}},
    {"A2", {"B3", "B4"}},
    {"A3", {"B5", "B6"}},

    {"B1", {"A1", "A2"}},
    {"B2", {"A3", "C1"}},
    {"B3", {"C2", "C3"}},
    {"B4", {"C4", "E1"}},
    {"B5", {"E2", "E3"}},
    {"B6", {"E4", "E5"}},

    {"C1", {"B1", "B2"}},
    {"C2", {"B3", "B4"}},
    {"C3", {"B5", "B6"}},
    {"C4", {"D1", "D2"}},

    {"D1", {"C1", "C2"}},
    {"D2", {"C3", "C4"}},

    {"E1", {"B1", "B2"}},
    {"E2", {"B3", "B4"}},
    {"E3", {"B5", "B6"}},
    {"E4", {"F1", "F2"}},
    {"E5", {"F3", "F4"}},

    {"F1", {"E1", "E2"}},
    {"F2", {"E3", "E4"}},
    {"F3", {"E5", "G1"}},
    {"F4", {"G2", "G3"}},

    {"G1", {"F1", "F2"}},
    {"G2", {"F3", "F4"}},
    {"G3", {"H1", "H2"}},

    {"H1", {"G1", "G2"}},
    {"H2", {"G3", "I1"}},

    {"I1", {"H1", "H2"}}
  };
  const string add = "ABEFGH";
  string op, s;
  int n;
  cin >> op >> n >> s;
  if (op == "encode") {
    string v = "";
    v += add.back();
    v += "1";
    string res = "";
    for (int i = 0; i < n; i++) {
      if (s[i] == '0') {
        v = edges[v].first;
        res += v[0];
      } else {
        v = edges[v].second;
        res += v[0];
      }
    }
    string id = add.substr((int)add.size() - int(v[1] - '0'), int(v[1] - '0'));
    cout << id + res << '\n';
  } else {
    map<string, map<char, pair<char, string>>> recover;
    for (auto it = edges.begin(); it != edges.end(); it++) {
      recover[it->second.first][it->first[0]] = {'0', it->first};
      recover[it->second.second][it->first[0]] = {'1', it->first};
    }
    int j = 0;
    while (j < n && s[j] != add.back()) {
      j++;
    }
    n -= j;
    s = s.substr(j);
    string v = "";
    v += s.back();
    v += char('1' + j);
    string res = "";
    for (int i = n - 2; i >= 0; i--) {
      res += recover[v][s[i]].first;
      v = recover[v][s[i]].second;
    }
    reverse(res.begin(), res.end());
    cout << res << '\n';
  }
}
accuracy: 1
time_ms: 60
memory_kb: 5012
final_rank: 3

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

int main() {
  map<string, pair<string, string>> edges = {
    {"A1", {"B1", "B2"}},
    {"A2", {"B3", "B4"}},
    {"A3", {"B5", "B6"}},

    {"B1", {"A1", "A2"}},
    {"B2", {"A3", "C1"}},
    {"B3", {"C2", "C3"}},
    {"B4", {"C4", "E1"}},
    {"B5", {"E2", "E3"}},
    {"B6", {"E4", "E5"}},

    {"C1", {"B1", "B2"}},
    {"C2", {"B3", "B4"}},
    {"C3", {"B5", "B6"}},
    {"C4", {"D1", "D2"}},

    {"D1", {"C1", "C2"}},
    {"D2", {"C3", "C4"}},

    {"E1", {"B1", "B2"}},
    {"E2", {"B3", "B4"}},
    {"E3", {"B5", "B6"}},
    {"E4", {"F1", "F2"}},
    {"E5", {"F3", "F4"}},

    {"F1", {"E1", "E2"}},
    {"F2", {"E3", "E4"}},
    {"F3", {"E5", "G1"}},
    {"F4", {"G2", "G3"}},

    {"G1", {"F1", "F2"}},
    {"G2", {"F3", "F4"}},
    {"G3", {"H1", "H2"}},

    {"H1", {"G1", "G2"}},
    {"H2", {"G3", "I1"}},

    {"I1", {"H1", "H2"}}
  };
  const string add = "ABEFGH";
  string op, s;
  int n;
  cin >> op >> n >> s;
  if (op[0] == 'e') {
    string v = "";
    v += add.back();
    v += "1";
    string res = "";
    for (int i = 0; i < n; i++) {
      if (s[i] == '0') {
        v = edges[v].first;
        res += v[0];
      } else {
        v = edges[v].second;
        res += v[0];
      }
    }
    string id = add.substr((int)add.size() - int(v[1] - '0'), int(v[1] - '0'));
    cout << id + res << '\n';
  } else {
    map<string, map<char, pair<char, string>>> recover;
    for (auto it = edges.begin(); it != edges.end(); it++) {
      recover[it->second.first][it->first[0]] = {'0', it->first};
      recover[it->second.second][it->first[0]] = {'1', it->first};
    }
    int j = 0;
    while (j < n && s[j] != add.back()) {
      j++;
    }
    n -= j;
    s = s.substr(j);
    string v = "";
    v += s.back();
    v += char('1' + j);
    string res = "";
    for (int i = n - 2; i >= 0; i--) {
      res += recover[v][s[i]].first;
      v = recover[v][s[i]].second;
    }
    reverse(res.begin(), res.end());
    cout << res << '\n';
  }
}
accuracy: 1
time_ms: 60
memory_kb: 4984
final_rank: 2

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

int main() {
  map<string, pair<string, string>> edges = {
    {"A1", {"B1", "B2"}},
    {"A2", {"B3", "B4"}},
    {"A3", {"B5", "B6"}},

    {"B1", {"A1", "A2"}},
    {"B2", {"A3", "C1"}},
    {"B3", {"C2", "C3"}},
    {"B4", {"C4", "E1"}},
    {"B5", {"E2", "E3"}},
    {"B6", {"E4", "E5"}},

    {"C1", {"B1", "B2"}},
    {"C2", {"B3", "B4"}},
    {"C3", {"B5", "B6"}},
    {"C4", {"D1", "D2"}},

    {"D1", {"C1", "C2"}},
    {"D2", {"C3", "C4"}},

    {"E1", {"B1", "B2"}},
    {"E2", {"B3", "B4"}},
    {"E3", {"B5", "B6"}},
    {"E4", {"F1", "F2"}},
    {"E5", {"F3", "F4"}},

    {"F1", {"E1", "E2"}},
    {"F2", {"E3", "E4"}},
    {"F3", {"F5", "G1"}},
    {"F4", {"G2", "G3"}},

    {"G1", {"F1", "F2"}},
    {"G2", {"F3", "F4"}},
    {"G3", {"H1", "H2"}},

    {"H1", {"G1", "G2"}},
    {"H2", {"G3", "I1"}},

    {"I1", {"H1", "H2"}}
  };
  const string add = "HGFEBA";
  string op, s;
  int n;
  cin >> op >> n >> s;
  if (op[0] == 'e') {
    string v = "";
    v += add.back();
    v += "1";
    string res = "";
    for (int i = 0; i < n; i++) {
      if (s[i] == '0') {
        v = edges[v].first;
        res += v[0];
      } else {
        v = edges[v].second;
        res += v[0];
      }
    }
    string id = add.substr((int)add.size() - int(v[1] - '0'), int(v[1] - '0'));
    cout << id + res << '\n';
  } else {
    map<string, map<char, pair<char, string>>> recover;
    for (auto it = edges.begin(); it != edges.end(); it++) {
      recover[it->second.first][it->first[0]] = {'0', it->first};
      recover[it->second.second][it->first[0]] = {'1', it->first};
    }
    int j = 0;
    while (j < n && s[j] != add.back()) {
      j++;
    }
    n -= j;
    s = s.substr(j);
    string v = "";
    v += s.back();
    v += char('1' + j);
    string res = "";
    for (int i = n - 2; i >= 0; i--) {
      res += recover[v][s[i]].first;
      v = recover[v][s[i]].second;
    }
    reverse(res.begin(), res.end());
    cout << res << '\n';
  }
}
accuracy: 0.024390243902439025
time_ms: 10
memory_kb: 4632
final_rank: 5

code_snippet: #include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
//#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<array>
#include<map>
#include<iomanip>
#include<assert.h>
#include<bitset>
#include<stack>
#include<memory>
using namespace std;
//using namespace std::chrono;
typedef long long int llint;
typedef double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase

/*
cout<<fixed<<setprecision(20);
cin.tie(0);
ios::sync_with_stdio(false);
*/
const llint mod=1000000007;
const llint big=2.19e15+1;
const long double pai=3.141592653589793238462643383279502884197;
const long double eps=1e-7;
template <class T,class U>bool mineq(T& a,U b){if(a>b){a=b;return true;}return false;}
template <class T,class U>bool maxeq(T& a,U b){if(a<b){a=b;return true;}return false;}
llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}
llint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;}
template<class T> void SO(T& ve){sort(ve.begin(),ve.end());}
template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());}
template<class T>llint LBI(vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();}
template<class T>llint UBI(vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();}
int kazu[9]={3,6,4,2,5,4,3,2,1};
int go[9][12]={
	{
		10,13,
		11,14,
		12,15,
	},{
		00,23,
		01,40,
		02,41,
		20,42,
		21,43,
		22,44,
	},{
		30,12,
		31,13,
		10,14,
		11,15,
	},{
		20,22,
		21,23,
	},{
		10,15,
		11,50,
		12,51,
		13,52,
		14,53,
	},{
		40,44,
		41,60,
		42,61,
		43,62,
	},{
		50,53,
		51,70,
		52,71,
	},{
		60,62,
		61,80,
	},{
		70,71,
	}
};
void encode(void){
	int n;cin>>n;
	string s;cin>>s;
	s+="111111111";
	int bas=8,num=0;
	cout<<"I";
	for(auto it:s){
		int suu=go[bas][num*2+it-'0'];
		bas=suu/10;num=suu%10;
		cout<<(char)(bas+'A');
	}
}
void decode(void){
	int i,j,m;cin>>m;
	string t;cin>>t;
	int bas=10,num=0;
	string ans;ans.reserve(m);
	for(i=m-1;i>=0;i--){
		if(bas==10&&t[i]=='I'){bas=8;num=0;continue;}
		if(bas==10){continue;}
		int doc=t[i]-'A';
		for(j=0;j<kazu[doc]*2;j++){
			if(go[doc][j]==bas*10+num){
				bas=doc;num=j/2;
				if(i<m-10){ans.pub('0'+j%2);}
				break;
			}
		}
	}
	REV(ans);
	cout<<ans<<endl;
}
int main(void){
	string str;cin>>str;
	if(str=="decode"){decode();}
	else{encode();}
	return 0;
}
/*
IHGFGFEFGFGHIHIH
*/
accuracy: 1
time_ms: 80
memory_kb: 4716
final_rank: 4

code_snippet: #include <iostream>
#include <map>
#include <string>
#include <algorithm>
//#include <ctime>
using namespace std;
#define M(x,y,z) make_pair((x),make_pair((y),(z)))
typedef map<string,pair<string,string> > mspss;
typedef map<string,map<char,pair<char,string> > >  msmcpcs;
//http://www.kupc.jp/2013/editorial/K.pdf
mspss table1={
	M("A1","B1","B2"),M("A2","B3","B4"),M("A3","B5","B6"),
	M("B1","A1","A2"),M("B2","A3","C1"),M("B3","C2","C3"),M("B4","C4","E1"),M("B5","E2","E3"),M("B6","E4","E5"),
	M("C1","B1","B2"),M("C2","B3","B4"),M("C3","B5","B6"),M("C4","D1","D2"),
	M("D1","C1","C2"),M("D2","C3","C4"),
	M("E1","B1","B2"),M("E2","B3","B4"),M("E3","B5","B6"),M("E4","F1","F2"),M("E5","F3","F4"),
	M("F1","E1","E2"),M("F2","E3","E4"),M("F3","E5","G1"),M("F4","G2","G3"),
	M("G1","F1","F2"),M("G2","F3","F4"),M("G3","H1","H2"),
	M("H1","G1","G2"),M("H2","G3","I1"),
	M("I1","H1","H2"),
};
msmcpcs table2;
string trailing_paths[]={
	"HGFEBA","IHGFEB","BABABC","ABABCD","BABABE","ABABEF","BABEFG","ABEFGH","BEFGHI"
};
int main(){
	string trailing_path=trailing_paths[0];//time(NULL)/86400*86400%9]; //just for fun :p
	string s,t,r;
	int n,i=0;
	cin>>s>>n>>t;
	if(s[0]=='e'){
		// n=8
		// t=10111110
		s=string(1,trailing_path.back())+string(1,'1');
		for(i=0;i<n;i++){
			if(t[i]=='0')r+=(s=table1[s].first)[0];
			else r+=(s=table1[s].second)[0];
		}
		// s=G3
		// r=BABEFGHG
		t=trailing_path.substr(trailing_path.size()-(s[1]-'0'),s[1]-'0');
		// t=EBA
		cout<<t+r<<endl;
	}else{
		mspss::iterator it=table1.begin();
		for(;it!=table1.end();it++){
			table2[it->second.first][it->first[0]]=make_pair('0',it->first);
			table2[it->second.second][it->first[0]]=make_pair('1',it->first);
		}
		// n=11
		// t=EBABABEFGHG
		for(i=0;t[i]!=trailing_path.back();i++);
		n-=i;
		t=t.substr(i);
		s=string(1,t[n-1])+string(1,'1'+i);
		// n=9
		// t=ABABEFGHG
		// s=G3
		for(i=n-2;i>=0;i--){
			r+=table2[s][t[i]].first;
			s=table2[s][t[i]].second;
		}
		reverse(r.begin(),r.end());
		cout<<r<<endl;
	}
}
accuracy: 1
time_ms: 150
memory_kb: 2720
final_rank: 1