Problem:
Problem E:
Reading a Chord
In this problem, you are required to write a program that enumerates all chord names for given
tones.
We suppose an ordinary scale that consists of the following 12 tones:
C, C
#
, D, D
#
, E, F, F
#
, G, G
#
, A, A
#
, B
Two adjacent tones are different by a
half
step; the right one is higher. Hence, for example, the
tone G is higher than the tone E by three half steps. In addition, the tone C is higher than the
tone B by a half step. Strictly speaking, the tone C of the next octave follows the tone B, but
octaves do not matter in this problem.
A chord consists of two or more different tones, and is called by its
chord name
. A chord may be
represented by multiple chord names, but each chord name represents exactly one set of tones.
In general, a chord is represented by a basic chord pattern (
base chord
) and additional tones
(
tension
) as needed. In this problem, we consider five basic patterns as listed below, and up to
one additional tone.
Figure 1: Base chords (only shown those built on C)
The chords listed above are built on the tone C, and thus their names begin with C. The tone
that a chord is built on (the tone C for these chords) is called the
root
of the chord.
A chord specifies its root by an absolute tone, and its other components by tones relative to its
root. Thus we can obtain another chord by shifting all components of a chord. For example, the
chord name D represents a chord consisting of the tones D, F
#
and A, which are the shifted-up
tones of C, E and G (which are components of the chord C) by two half steps.
An additional tone, a tension, is represented by a number that may be preceding a plus or
minus sign, and designated parentheses after the basic pattern. The figure below denotes which
number is used to indicate each tone.
Figure 2: Tensions for C chords
For example, C(9) represents the chord C with the additional tone D, that is, a chord that
consists of C, D, E and G. Similarly, C(+11) represents the chord C plus the tone F
#
.
The numbers that specify tensions denote relative tones to the roots of chords like the compo-
nents of the chords. Thus change of the root also changes the tones specified by the number, as
illustrated below.
Figure 3: Tensions for E chords
+5 and -5 are the special tensions. They do not indicate to add another tone to the chords,
but to sharp (shift half-step up) or flat (shift half-step down) the fifth tone (the tone seven half
steps higher than the root). Therefore, for example, C(+5) represents a chord that consists of
C, E and G
#
, not C, E, G and G
#
.
Figure 4 describes the syntax of chords in Backus-Naur Form.
Now suppose we find chord names for the tones C, E and G. First, we easily find the chord C
consists of the tones C, E and G by looking up the base chord table shown above. Therefore
‘C’ should be printed. We have one more chord name for those tones. The chord Em obviously
represents the set of tones E, G and B. Here, if you sharp the tone B, we have 
...(truncated)

Submitted solutions:

code_snippet: #include "bits/stdc++.h"

#pragma warning(disable:4996)
using namespace std;
string root_sts = "C ,C#,D ,D#,E ,F ,F#,G ,G#,A ,A#,B ";

int get_num(string name) {

	int k= root_sts.find(name);
	string l= root_sts.substr(0,k);
	return count(l.begin(),l.end(),',');
}
bool check(vector<int>a, vector<int>b) {
	for (auto& k : a) {
		k%=12;
	}
	for (auto& k : b) {
		k %= 12;
	}
	sort(a.begin(),a.end());
	sort(b.begin(),b.end());
	return a==b;
}

int main() {
	int Q;cin>>Q;
	while (Q--) {
		int N;cin>>N;
		vector<int>nums(N);
		for (int i = 0; i < N; ++i) {
			string name;cin>>name;nums[i]=get_num(name);
		}
		sort(nums.begin(),nums.end());

		vector<vector<int>>components = {
			vector<int>{0,4,7},
			vector<int>{0,4,7,10},

			vector<int>{0,4,7,11},

			vector<int>{0,3,7},
			vector<int>{0,3,7,10},

		};
		vector<string>chord_names = {
			"",
			"7",
			"M7",
			"m",
			"m7"
		};
		vector<string>anss;
		for (int root = 0; root < 12; ++root) {
			string root_name = root_sts.substr(root * 3, 2);

			if(root_name[1]==' ')root_name.pop_back();
			for (int chord = 0; chord < 5; ++chord) {
				string chord_name=chord_names[chord];
				string tension_name;
				{
					vector<string>tension_bases{
						"5",
						"9"
						,"11",
						"13"
					};
					for (int tension_part = 0; tension_part < 4; ++tension_part) {
						for (int plus = -1; plus <= 1; plus++) {
							string minus;
							if (plus == 1) {
								minus="+";
							}
							else if (plus == -1) {
								minus="-";
							}
							string tension_name="("+
								minus+tension_bases[tension_part]
								+")";
							vector<int>voices(components[chord]);
							if (tension_part == 0) {
								if (plus == 0) {
									tension_name="";
								}
								voices[2]+=plus;
							}
							else {
								if (tension_part == 1) {
									voices.push_back(2+plus);

								}
								else if (tension_part == 2) {
									voices.push_back(5+plus);
								}
								else if (tension_part == 3) {
									voices.push_back(9+plus);
								}
							}
							for(auto &vo:voices)vo+=root;
							if (check(voices, nums)) {
								string ans=root_name
									+chord_name+tension_name;
								anss.push_back(ans);
							}
						}
					}
					
				}
			}
		}
		if (anss.empty()) {
			cout<<"UNKNOWN"<<endl;
		}
		else {
			for (int i = 0; i < anss.size(); ++i) {
				cout<<anss[i];
				if(i==anss.size()-1)cout<<endl;
				else cout<<" ";
			}
		}
	}
	return 0;
}
accuracy: 1
time_ms: 430
memory_kb: 3016
final_rank: 10

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 12

struct Info{
	Info(int arg_tone_id,int arg_code_id,int arg_tension_op,int arg_tension_value,bool arg_is_tension){
		tone_id = arg_tone_id;
		code_id = arg_code_id;
		tension_op = arg_tension_op;
		tension_value = arg_tension_value;
		is_tension = arg_is_tension;
	}
	int tone_id,code_id,tension_op,tension_value;
	bool is_tension;
};

int POW[NUM];
int tension_array[4] = {9,11,13,5};
int diff_table[3][3] = {{1,2,3},{4,5,6},{8,9,10}};
char tone_name[NUM][3] = {"C","C#","D","D#","E","F","F#","G","G#","A","A#","B"};
char code_name[NUM][3] = {"@","7","M7","m","m7"};
vector<int> component[5];
vector<Info> ANS;

bool strCmp(char* base, char* comp){
	int length1,length2;
	for(length1=0;base[length1] != '\0';length1++);
	for(length2=0;comp[length2] != '\0';length2++);
	if(length1 != length2)return false;

	for(int i=0;base[i] != '\0'; i++){
		if(base[i] != comp[i])return false;
	}
	return true;
}

//該当するtoneのidを返却する
int get_id(char buf[3]){

	for(int i = 0; i < NUM; i++){
		if(strCmp(tone_name[i],buf)){
			return i;
		}
	}

	return -1;
}

int get_state(bool check[NUM]){

	int ret = 0;

	for(int i = 0; i < NUM; i++){
		if(check[i]){
			ret += POW[i];
		}
	}

	return ret;
}

void print_Info(Info info){

	printf("%s",tone_name[info.tone_id]);

	if(info.code_id != 0){

		printf("%s",code_name[info.code_id]);
	}
	if(!info.is_tension)return;

	printf("(");

	if(info.tension_op != 0){

		if(info.tension_op > 0){

			printf("+");

		}else{

			printf("-");
		}
	}
	printf("%d",info.tension_value);
	printf(")");
}

void dfs(int ans_state){

	int tmp,tmp_state,tmp_value,diff,tmp_loc;
	bool check[12],pre;

	for(int base_tone = 0; base_tone < NUM; base_tone++){
		for(int cord = 0; cord < 5; cord++){

			for(int i = 0; i < NUM; i++){
				check[i] = false;
			}
			for(int i = 0; i < component[cord].size(); i++){
				check[(base_tone+component[cord][i])%12] = true;
			}

			tmp_state = get_state(check);

			if(tmp_state == ans_state){
				ANS.push_back(Info(base_tone,cord,0,0,false)); //テンション無しで見つかればcontinue
				continue;
			}

			//テンションを全探索
			for(int t = 0; t < 4; t++){

				tmp_value = tension_array[t];

				if(tmp_value != 5){
					//各数字について、マイナス、ノーマル、プラスの3つ
					for(int a = 0; a < 3; a++){

						diff = diff_table[t][a]; //base_toneからの相対位置
						tmp_loc = (base_tone+diff)%12;
						pre = check[tmp_loc];
						check[tmp_loc] = true;
						tmp_state = get_state(check);
						if(tmp_state == ans_state){
							ANS.push_back(Info(base_tone,cord,a-1,tmp_value,true));
						}
						check[tmp_loc] = pre;
					}

				}else{ //tmp_value == 5

					//★base_toneより7半音高い音を上下させる
					tmp_loc = (base_tone+7)%12;
					check[tmp_loc] = false;

					//マイナス
					pre = check[(tmp_loc+11)%12];
					check[(tmp_loc+11)%12] = true; //-1の代わりに11
					tmp_state = get_state(check);
					if(tmp_state == ans_state){
						ANS.push_back(Info(base_tone,cord,-1,tmp_value,true));
					}
					check[(tmp_loc+11)%12] = pre;

					//プラス
					pre = check[(tmp_loc+1)%12];
					check[(tmp_loc+1)%12] = true;
					tmp_state = get_state(check);
					if(tmp_state == ans_state){
						ANS.push_back(Info(base_tone,cord,1,tmp_value,true));
					}
					check[(tmp_loc+1)%12] = pre;

					check[tmp_loc] = true;
				}
			}
		}
	}
}


void func(){

	int num;
	char buf[3];

	scanf("%d",&num);

	int tmp_state = 0;

	for(int loop = 0; loop < num; loop++){
		scanf("%s",buf);
		tmp_state += POW[get_id(buf)];
	}

	ANS.clear();

	dfs(tmp_state);

	if(ANS.size() == 0){
		printf("UNKNOWN\n");
		return;
	}

	print_Info(ANS[0]);
	for(int i = 1; i < ANS.size(); i++){
		printf(" ");
		print_Info(ANS[i]);
	}

	printf("\n");
}


int main(){

	//何もなし
	component[0].push_back(0);
	component[0].push_back(4);
	component[0].push_back(7);

	//7
	component[1].push_back(0);
	component[1].push_back(4);
	component[1].push_back(7);
	component[1].push_back(10);

	//M7
	component[2].push_back(0);
	component[2].push_back(4);
	component[2].push_back(7);
	component[2].push_back(11);

	//m
	component[3].push_back(0);
	component[3].push_back(3);
	component[3].push_back(7);

	//m7
	component[4].push_back(0);
	component[4].push_back(3);
	component[4].push_back(7);
	component[4].push_back(10);


	POW[0] = 1;
	for(int i = 1; i < NUM; i++){
		POW[i] = POW[i-1]*2;
	}
	int num_case;

	scanf("%d",&num_case);

	for(int loop = 0; loop < num_case; loop++){
		func();
	}

	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3164
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
 
int main() {
  string s[12]={"C","C#","D","D#","E","F","F#","G","G#","A","A#","B"};
  string t[5]={"","7","M7","m","m7"};
  string r[11]={"","-9","9","+9","-11","11","+11","","-13","13","+13"};
  int d[5][4]={{0,4,7,0},{0,4,7,10},{0,4,7,11},{0,3,7,0},{0,3,7,10}};
  int T;
  cin >> T;
  while(T--) {
    int n;
    cin >> n;
    vector<int> a(12,0);
    for(int i=0; i<n; i++) {
      string e;
      cin >> e;
      for(int j=0; j<12; j++) {
        if(s[j]==e) a[j]=1;
      }
    }
    vector<string> ans;
    for(int i=0; i<12; i++) {
      for(int j=0; j<5; j++) {
        vector<int> b(12,0);
        for(int k=0; k<4; k++) b[(i+d[j][k])%12]=1;
        if(a==b) {
          ans.push_back(s[i]+t[j]);
          continue;
        }
        for(int k=1; k<11; k++) {
          if(k==7) continue;
          int x=b[(i+k)%12];
          b[(i+k)%12]=1;
          if(a==b) ans.push_back(s[i]+t[j]+"("+r[k]+")");
          b[(i+k)%12]=x;
        }
        b[(i+7)%12]=0;b[(i+6)%12]=1;
        if(a==b) ans.push_back(s[i]+t[j]+"(-5)");
        b[(i+6)%12]=0;b[(i+8)%12]=1;
        if(a==b) ans.push_back(s[i]+t[j]+"(+5)");
      }
    }
    if(ans.size()) {
      for(int i=0; i<ans.size(); i++) {
        if(i) cout << " ";
        cout << ans[i];
      }
      cout << endl;
    } else cout << "UNKNOWN" << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3148
final_rank: 6

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

int main() {
  string s[12]={"C","C#","D","D#","E","F","F#","G","G#","A","A#","B"};
  string t[5]={"","7","M7","m","m7"};
  string r[11]={"","-9","9","+9","-11","11","+11","","-13","13","+13"};
  int d[5][4]={{0,4,7,-1},{0,4,7,10},{0,4,7,11},{0,3,7,-1},{0,3,7,10}};
  int T;
  cin >> T;
  while(T--) {
    int n;
    cin >> n;
    vector<int> a(12,0);
    for(int i=0; i<n; i++) {
      string e;
      cin >> e;
      for(int j=0; j<12; j++) {
        if(s[j]==e) {
          a[j]=1;
          break;
        }
      }
    }
    vector<string> ans;
    for(int i=0; i<12; i++) {
      for(int j=0; j<5; j++) {
        vector<int> b(12,0);
        for(int k=0; k<4; k++) {
          if(d[j][k]==-1) break;
          b[(i+d[j][k])%12]=1;
        }
        if(a==b) {
          ans.push_back(s[i]+t[j]);
          continue;
        }
        for(int k=1; k<11; k++) {
          if(k==7) continue;
          int x=b[(i+k)%12];
          b[(i+k)%12]=1;
          if(a==b) ans.push_back(s[i]+t[j]+"("+r[k]+")");
          b[(i+k)%12]=x;
        }
        b[(i+7)%12]=0;b[(i+6)%12]=1;
        if(a==b) ans.push_back(s[i]+t[j]+"(-5)");
        b[(i+6)%12]=0;b[(i+8)%12]=1;
        if(a==b) ans.push_back(s[i]+t[j]+"(+5)");
      }
    }
    if(ans.size()) {
      for(int i=0; i<ans.size(); i++) {
        if(i) cout << " ";
        cout << ans[i];
      }
      cout << endl;
    } else cout << "UNKNOWN" << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3132
final_rank: 5

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
#include<sstream>

using namespace std;

struct Code{
  string name;
  vector<string> sound;
};

const int KIND = 12;
string sound[] = {"C","C#","D","D#","E","F","F#","G","G#","A","A#","B"};

vector<string> make_vec(const string& s){
  stringstream ss(s);
  vector<string> res;
  string str;
  while(ss >> str) res.push_back(str);
  return res;
}

int get_index(const string& s){
  for(int i = 0; i < KIND; i++) if(sound[i] == s) return i;
  return -1;
}

Code shift(Code c, int n){
  c.name = sound[n] + c.name.substr(1);
  
  vector<string> tmp(c.sound.size());
  for(int i = 0; i < (int)tmp.size(); i++){
    int idx = get_index(c.sound[i]);
    tmp[i] = sound[(idx+n)%KIND];
  }
  sort(tmp.begin(),tmp.end());
  c.sound = tmp;
  return c;
}

string to_str(int n){
  ostringstream oss;
  oss << n;
  return oss.str();
}

vector<string> get_sound(int n){
  if(n == 5) return make_vec("F# G G#");
  if(n == 9) return make_vec("C# D D#");
  if(n == 11) return make_vec("E F F#");
  if(n == 13) return make_vec("G# A A#");
  return make_vec("ERROR");
}

Code change_tension(Code c, int n, int type){
  vector<string> v = get_sound(n);
  int pos = -1;
  for(int i = 0; i < (int)c.sound.size(); i++)
    for(int j = 0; j < (int)v.size(); j++)
      if(c.sound[i] == v[j]) pos = i;
  
  string op = "";
  if(type == 0) op = "-";
  if(type == 2) op = "+";

  c.name += "(" + op + to_str(n) + ")";
  
  if(n == 5){
    if(pos == -1) c.sound.push_back(v[type]);
    else c.sound[pos] = v[type];

  }else{
    if(pos == -1 || c.sound[pos] != v[type]) c.sound.push_back(v[type]);
  }
  //  if(pos == -1 || c.sound[pos] != v[type]) c.sound.push_back(v[type]);

  sort(c.sound.begin(), c.sound.end());
  return c;
}

void print(const vector<Code>& v){
  cout << "--------- begin -----------" << endl;
  
  for(int i = 0; i < (int)v.size(); i++){
    cout << v[i].name << endl;
    for(int j = 0; j < (int)v[i].sound.size(); j++) cout << v[i].sound[j] << " ";
    cout << endl;
  }
  cout << "--------- end -----------" << endl;
}

vector<Code> make(){
  vector<Code> res, tmp;
  Code base[5];
  base[0] = (Code){"C", make_vec("C E G")};
  base[1] = (Code){"C7",make_vec("C E G A#")};
  base[2] = (Code){"CM7", make_vec("C E G B")};
  base[3] = (Code){"Cm", make_vec("C D# G")};
  base[4] = (Code){"Cm7", make_vec("C D# G A#")};

  for(int i = 0; i < 5; i++){
    sort(base[i].sound.begin(), base[i].sound.end());
    res.push_back(base[i]);
    tmp.push_back(base[i]);
  }
  
  int tens[] = {5,9,11,13};
  for(int i = 0; i < 5; i++)
    for(int j = 0; j < 4; j++)
      for(int k = 0; k < 3; k++){
	Code nex = change_tension(base[i], tens[j], k);
	if(nex.sound != base[i].sound){
	  res.push_back(nex);
	  tmp.push_back(nex);
	}
      }
  
  for(int i = 0; i < (int)tmp.size(); i++)
    for(int j = 1; j < 12; j++) res.push_back(shift(tmp[i], j));
   
  for(int i = 0; i < (int)res.size(); i++) sort(res[i].sound.begin(), res[i].sound.end());
  return res;
}

vector<string> solve(const vector<Code>& cand, const vector<string>& v){

  vector<string> res;
  for(int i = 0; i < (int)cand.size(); i++)
    if(cand[i].sound == v) res.push_back(cand[i].name);
  return res;
}

void TEST(const vector<Code>& cand){

  for(int i = 0; i < (int)cand.size(); i++)
    if(cand[i].name == "Dm7(9)"){
      cout << cand[i].name << endl;
      for(int j = 0; j < (int)cand[i].sound.size(); j++)
	cout << cand[i].sound[j] << " " ;
      cout << endl;
    }
  exit(0);
}

int main(){

  vector<Code> cand = make();
  // TEST(cand);
 
  int T;
  cin >> T;
  while(T--){
    int n;
    cin >> n;
    vector<string> v(n);
    for(int i = 0; i < n; i++) cin >> v[i];
    sort(v.begin(), v.end());
    vector<string> ans = solve(cand, v);
    if(ans.size() == 0) cout << "UNKNOWN" << endl;
    else{
      for(int i = 0; i < (int)ans.size(); i++){
	if(i) cout << " ";
	cout << ans[i];
      }
      cout << endl;
    }
  }

  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1380
final_rank: 3

code_snippet: #include <iostream>
#include <cstdio>
#include <vector>
#include <list>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <iterator>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <stack>
#include <climits>
#include <deque>
#include <bitset>
#include <cassert>
#include <ctime>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
const int dy[]={-1,0,1,0},dx[]={0,1,0,-1};
// adjust problem by problem
const double EPS=1e-8;
const double PI=acos(-1.0);
#ifdef __GNUC__
int popcount(int n){return __builtin_popcount(n);}
int popcount(ll n){return __builtin_popcountll(n);}
#endif
#ifndef __GNUC__
template<class T> int popcount(T n){int cnt=0;while(n){if(n%2)cnt++;n/=2;}return cnt;}
#endif
template<class T>int SIZE(T a){return a.size();}
template<class T>string IntToString(T num){string res;stringstream ss;ss<<num;return ss.str();}
template<class T>T StringToInt(string str){T res=0;for(int i=0;i<SIZE(str);i++)res=(res*10+str[i]-'0');return res;}
template<class T>T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
template<class T>T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> void PrintSeq(T &a,int sz){for(int i=0;i<sz;i++){cout<<a[i];if(sz==i+1)cout<<endl;else cout<<' ';}}
bool EQ(double a,double b){return abs(a-b)<EPS;}
void fastStream(){cin.tie(0);std::ios_base::sync_with_stdio(0);}
vector<string> split(string str,char del){
  vector<string> res;
  for(int i=0,s=0;i<SIZE(str);i++){
    if(str[i]==del){if(i-s!=0)res.push_back(str.substr(s,i-s));s=i+1;}
    else if(i==SIZE(str)-1){res.push_back(str.substr(s));}
  }
  return res;
}


int main(){
  map<string,int> toneToIdx;
  string idxToTone[]={"C","C#","D","D#","E","F","F#","G","G#","A","A#","B"};
  // コードの名前とそのcomponent
  for(int i=0;i<12;i++)toneToIdx[idxToTone[i]]=i;
  map<string,set<int> > chords;
  {
    set<int> st;
    st.insert(toneToIdx["C"]);
    st.insert(toneToIdx["E"]);
    st.insert(toneToIdx["G"]);
    chords["C"]=st;
  }
  {
    set<int> st;
    st.insert(toneToIdx["C"]);
    st.insert(toneToIdx["E"]);
    st.insert(toneToIdx["G"]);
    st.insert(toneToIdx["A#"]);
    chords["C7"]=st;
  }
  {
    set<int> st;
    st.insert(toneToIdx["C"]);
    st.insert(toneToIdx["E"]);
    st.insert(toneToIdx["G"]);
    st.insert(toneToIdx["B"]);
    chords["CM7"]=st;
  }
  {
    set<int> st;
    st.insert(toneToIdx["C"]);
    st.insert(toneToIdx["D#"]);
    st.insert(toneToIdx["G"]);
    chords["Cm"]=st;
  }
  {
    set<int> st;
    st.insert(toneToIdx["C"]);
    st.insert(toneToIdx["D#"]);
    st.insert(toneToIdx["G"]);
    st.insert(toneToIdx["A#"]);
    chords["Cm7"]=st;
  }

  typedef pair<string,string> pss;
  pss pars[]={pss("-9","C#"),pss("9","D"),pss("+9","D#")
              ,pss("-11","E"),pss("11","F"),pss("+11","F#")
              ,pss("-13","G#"),pss("13","A"),pss("+13","A#")
              ,pss("-5",""),pss("+5",""),pss("","")};              
  int N;
  cin>>N;
  while(N--){
    int M;
    set<int> inSets;
    vector<string> ress;
    cin>>M;
    for(int i=0;i<M;i++){
      string s;
      cin>>s;
      inSets.insert(toneToIdx[s]);
      //cout<<toneToIdx[s]<<" ";
    }
    //cout<<endl;
    // すべてのtoneをrootに
    for(int i=0;i<12;i++){
      // 5種類のコードを使用
      for(map<string,set<int> >::iterator it=chords.begin();it!=chords.end();it++){
        // 括弧の中身をどうするか
        for(int j=0;j<12;j++){
          string r=idxToTone[i]+it->first.substr(1);
          if(pars[j].first!="")r+="("+pars[j].first+")";
          //cout<<r<<endl;
          set<int> nowSt;
          for(set<int>::iterator iit=it->second.begin();iit!=it->second.end();iit++)
            nowSt.insert((*iit+i)%12);
          if(pars[j].first==""){}
          else if(pars[j].first[1]=='5'){
            // replace
            int tgt=(i+7)%12;
            // assert(it->second.size()==nowSt.size());
            // for(set<int>::iterator iit=nowSt.begin();iit!=nowSt.end();iit++){
            //   cout<<*iit<<" ";
            // }
            // cout<<endl;
            nowSt.erase(tgt);
            if(pars[j].first[0]=='+')nowSt.insert((tgt+1)%12);
            else nowSt.insert((tgt+11)%12);
          }
          // 普通に処理
          else{
            if(nowSt.count((i+toneToIdx[pars[j].second])%12))continue;
            nowSt.insert((i+toneToIdx[pars[j].second])%12);
          }
          if(inSets==nowSt){
            ress.push_back(r);
          }
        }
      }
    }
    if((int)ress.size()==0)cout<<"UNKNOWN"<<endl;
    else{
      for(int i=0;i<(int)ress.size();i++){
        cout<<ress[i];
        if(i==(int)ress.size()-1)cout<<endl;
        else cout<<" ";
      }
    }
  }
  
  return 0;
}
accuracy: 1
time_ms: 620
memory_kb: 1232
final_rank: 9

code_snippet: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
string tone[12] = {
  "C",
  "C#",
  "D",
  "D#",
  "E",
  "F",
  "F#",
  "G",
  "G#",
  "A",
  "A#",
  "B"
};
int to_int(string s){
  REP(i, 12) if(s == tone[i]) return i;
  assert(false);
  return -1;
}
string to_str(int n){
  assert(n >= 0 && n < 12);
  return tone[n];
}
string code_name[5] = {
  "",
  "7",
  "M7",
  "m",
  "m7"
};
int code_tone[][5] = {
  {0, 4, 7, -1, -1},
  {0, 4, 7, 10, -1},
  {0, 4, 7, 11, -1},
  {0, 3, 7, -1, -1},
  {0, 3, 7, 10, -1}
};
string tension_name[12] = {
  "",
  "(-9)",
  "(9)",
  "(+9)",
  "(-11)",
  "(11)",
  "(+11)",
  "",
  "(-13)",
  "(13)",
  "(+13)",
  "",
};
string special_tension[2] = {
  "(-5)",
  "(+5)"
};

vector<string> solve(vector<string> tone_str){
  vector<string> answer;
  vector<int> tones;
  REP(i, tone_str.size()) tones.push_back(to_int(tone_str[i]));
  REP(i, tones.size()){
    int offset = tones[i];
    vector<int> relative = tones;
    REP(i, relative.size()) relative[i] = (relative[i] - offset + 12) % 12;
    sort(relative.begin(), relative.end());
    REP(j, 5){
      vector<int> code;
      for(int k = 0; code_tone[j][k] != -1; k++){
        code.push_back(code_tone[j][k]);
      }
      if(code == relative) answer.push_back(to_str(offset) + code_name[j]);
      for(int k = 0; k < 2; k++){
        vector<int> code2 = code;
        code2.erase(remove(code2.begin(), code2.end(), 7)); code2.push_back(7 + (2 * k - 1));
        sort(code2.begin(), code2.end());
        if(code2 == relative) answer.push_back(to_str(offset) + code_name[j] + special_tension[k]);
      }
      for(int k = 0; k < 12; k++) if(tension_name[k] != ""){
        vector<int> code2 = code;
        code2.push_back(k);
        sort(code2.begin(), code2.end());
        if(code2 == relative) answer.push_back(to_str(offset) + code_name[j] + tension_name[k]);
      }
    }
  }
  return answer;
}

int main(){
  int T;
  cin >> T;
  while(T--){
    int R; cin >> R;
    vector<string> tone_str(R);
    REP(i, R) cin >> tone_str[i];
    vector<string> codes = solve(tone_str);

    if(codes.empty()) cout << "UNKNOWN" << endl;

    REP(i, codes.size()){
      cout << codes[i];
      if(i == codes.size() - 1) cout << endl;
      else cout << " ";
    }
  }
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 1240
final_rank: 4

code_snippet: #include <iostream>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

int main() {
  string tones[12] = {"C","C#","D","D#","E","F","F#","G","G#","A","A#","B"};
  int component[5][3] = {{4,7,-1},{4,7,10},{4,7,11},{3,7,-1},{3,7,10}};
  string compo[5] = {"","7","M7","m","m7"};
  int tension[12] = {0,1,2,3,4,5,6,-1,1,8,9,10};
  string tens[12] = {"","(-9)","(9)","(+9)","(-11)","(11)","(+11)","(-5)","(+5)","(-13)","(13)","(+13)"};

  map<set<int>, vector<string> > mp;
  REP(i, 12) {
    REP(j, 5) {
      vector<int> tmp;
      tmp.push_back(i);
      REP(k, 3) {
        if (component[j][k] == -1) break;
        tmp.push_back((i+component[j][k])%12);
      }
      map<set<int>,bool> memo;
      REP(k,12) {
        vector<int> tmp2(tmp);
        if (k == 0);
        else if (k == 7 || k == 8)   // +-5
          tmp2[2] = (tmp2[2]+tension[k]+12)%12;
        else
          tmp2.push_back((i+tension[k])%12);
        set<int> tmpset(ALL(tmp2));
        if (memo[tmpset]) continue;
        memo[tmpset] = 1;
        string s = tones[i];
        s += compo[j];
        s += tens[k];
        mp[tmpset].push_back(s);
        // cout << s << endl;
        // FOR(it, tmpset) cout << tones[*it] << " ";
        // cout << endl;
      }
    }
  }
  map<string, int> mp2;
  REP(i, 12) {
    mp2[tones[i]] = i;
  }
  int N;
  cin >> N;
  while(N--) {
    int n;
    cin >> n;
    set<int> se;
    REP(i,n) {
      string s;
      cin >> s;
      se.insert(mp2[s]);
    }
    vector<string> v = mp[se];
    if (v.size() == 0) {
      cout << "UNKNOWN" << endl;
    } else {
      REP(i, v.size()) {
        if (i) cout << " ";
        cout << v[i];
      }
      cout << endl;
    }
  }
}
accuracy: 1
time_ms: 10
memory_kb: 0
final_rank: 1

code_snippet: #include<iostream>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)
#define pb         push_back
#define ALL(C)     (C).begin(),(C).end()

const int CONST = 12;

bool have_same(vector<int> &a){
  rep(i,a.size())REP(j,i+1,a.size())if (a[i] == a[j])return true;
  return false;
}

void generate(vector<string> & a,vector<vector<int> > &b){
  vector<int> base[5];
  string    opdata[5]={"","7","M7","m","m7"};
  base[0].pb(0);base[0].pb(4);base[0].pb(7);
  base[1].pb(0);base[1].pb(4);base[1].pb(7);base[1].pb(10);
  base[2].pb(0);base[2].pb(4);base[2].pb(7);base[2].pb(11);
  base[3].pb(0);base[3].pb(3);base[3].pb(7);
  base[4].pb(0);base[4].pb(3);base[4].pb(7);base[4].pb(10);

  vector<int> pat;

  string option="";
  string ori[]={"C","C#","D","D#","E","F","F#","G","G#","A","A#","B"};
  string add[]={"-9","9","+9","-11","11","+11","-13","13","+13"};
  int   addp[]={1,2,3,4,5,6,8,9,10};
  string   h[]={"-5","+5"};
  int     hp[]={11,1};
  

  rep(k,5){
    pat=base[k];
    option=opdata[k];
    rep(i,12){      
      vector<int> cpy=pat;
      rep(j,pat.size())cpy[j]=(cpy[j]+i)%CONST;
      a.pb(ori[i]+option);
      b.pb(cpy);
      vector<int> cpy2=cpy;
      cpy2.pb(-1);
      rep(j,9){
	cpy2[cpy2.size()-1]=(i+addp[j])%CONST;
	if (have_same(cpy2))continue;
	a.pb(ori[i]+option+"("+add[j]+")");
	b.pb(cpy2);
      }
      
      int base=cpy[2];
      rep(j,2){
	//cpy[cpy.size()-1]=(base+hp[j])%CONST;
	cpy[2]=(base+hp[j])%CONST;
	a.pb(ori[i]+option+"("+h[j]+")");
	b.pb(cpy);
      }
    }
  }
   
  rep(i,a.size()){
    //    cout << a[i] << " ";// << b[i].size() << endl;
    //    rep(j,b[i].size())cout << b[i][j]<<" ";cout <<endl;
    sort(ALL(b[i]));
  }
}


bool  is_same_vector(vector<int> &a,vector<int> &b){
  if (a.size() != b.size())return false;
  rep(i,a.size())if (a[i] != b[i])return false;
  /*  
  cout << endl;
  cout <<"tete"<<endl;
  rep(i,a.size())cout << a[i];cout << endl;
  rep(i,b.size())cout << b[i];cout << endl;
  */  
  return true;
}

main(){
  vector<string> a;
  vector<vector<int> > b;
  generate(a,b);
  string ori[]={"C","C#","D","D#","E","F","F#","G","G#","A","A#","B"};
  map<string,int> M;
  rep(i,12)M[ori[i]]=i;
  
  int te;
  cin>>te;
  while(te--){
    int cnt = 0;
    int n;
    cin>>n;
    vector<int> in;
    rep(i,n){
      string tmp;cin>>tmp;in.pb(M[tmp]);
    }
    sort(ALL(in));
    
    //    rep(k,in.size()){
      rep(i,a.size()){
	if (is_same_vector(b[i],in)){
	  if (cnt)cout << " ";
	  cout << a[i];
	  cnt++;
	}
      }
      /*      
      int tmp = in[0];
      rep(i,in.size()-1)in[i]=in[i+1];
      in[in.size()-1]=tmp;
    }
      */
    if (cnt == 0)cout << "UNKNOWN";
    cout << endl;
  }
  return false;
}
accuracy: 1
time_ms: 10
memory_kb: 0
final_rank: 1

code_snippet: #include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

#define MP make_pair
#define pb push_back

int C[]={0,4,7};
int C7[]={0,4,7,10};
int CM7[]={0,4,7,11};
int Cm[]={0,3,7};
int Cm7[]={0,3,7,10};

map<int, string> d;
map<string, int> dic;
map<int, string> rev;

vector<string> ans;

void init()
{
	d[1]="-9";
	d[2]="9";
	d[3]="+9";
	d[4]="-11";
	d[5]="11";
	d[6]="+11";
	d[8]="-13";
	d[9]="13";
	d[10]="+13";
	
	dic["C"]	= 0;
	dic["C#"]	= 1;
	dic["D"]	= 2;
	dic["D#"]	= 3;
	dic["E"]	= 4;
	dic["F"]	= 5;
	dic["F#"]	= 6;
	dic["G"]	= 7;
	dic["G#"]	= 8;
	dic["A"]	= 9;
	dic["A#"]	= 10;
	dic["B"]	= 11;
	
	rev[0]	= "C";
	rev[1]	= "C#";
	rev[2]	= "D";
	rev[3]	= "D#";
	rev[4]	= "E";
	rev[5]	= "F";
	rev[6]	= "F#";
	rev[7]	= "G";
	rev[8]	= "G#";
	rev[9]	= "A";
	rev[10]	= "A#";
	rev[11]	= "B";
}

void ps(string s, int orig, string type)
{
	string pat;

	pat += rev[orig] + type;
	if(!s.empty()) pat += "("+ s + ")";

	ans.pb(pat);
}

bool check(vector<int> c, vector<int> p)
{
	if(c.size()!= p.size()) return false;

	sort(c.begin(), c.end());
	sort(p.begin(), p.end());
	
	for(int i=0; i<c.size(); i++)
		if(c[i]!=p[i]) return false;

	return true;
}

void ctension(vector<int> c, vector<int> p, int orig, string type)
{
	for(int i=1; i<=10; i++)
	{
		if(i==7) continue;
		vector<int> t(c);
		t.pb((orig+i)%12);
		if(check(t,p)) ps(d[i], orig, type);
	}

	vector<int>::iterator it=find(c.begin(), c.end(), (orig+7)%12);
	
	*it = ((*it)+11)%12;

	if(check(c,p)) ps("-5", orig, type);

	*it = ((*it)+2)%12;
	if(check(c,p)) ps("+5", orig, type);
}

void createC(vector<int> p)
{
	vector<int> c;
	for(int i=0; i<3; i++)
		c.pb(C[i]);

	for(int i=0; i<12; i++)
	{
		if(check(c,p)) ps("",i,"");
		ctension(c,p,i,"");
		
		for(int j=0; j<3; j++)
			c[j]=(c[j]+1)%12;
	}
}

void createC7(vector<int> p)
{

	vector<int> c;
	for(int i=0; i<4; i++)
		c.pb(C7[i]);

	for(int i=0; i<12; i++)
	{
		if(check(c,p)) ps("",i,"7");
		ctension(c,p,i,"7");
		
		for(int j=0; j<4; j++)
			c[j]=(c[j]+1)%12;
	}
}

void createCM7(vector<int> p)
{
	vector<int> c;
	for(int i=0; i<4; i++)
		c.pb(CM7[i]);

	for(int i=0; i<12; i++)
	{
		if(check(c,p)) ps("",i,"M7");
		ctension(c,p,i,"M7");
		
		for(int j=0; j<4; j++)
			c[j]=(c[j]+1)%12;
	}
}
void createCm(vector<int> p)
{
	vector<int> c;
	for(int j=0; j<3; j++)
		c.pb(Cm[j]);

	for(int i=0; i<12; i++)
	{
		if(check(c,p)) ps("",i,"m");
		ctension(c,p,i,"m");
		
		for(int j=0; j<3; j++)
			c[j]=(c[j]+1)%12;

	}
	
}

void createCm7(vector<int> p)
{
	vector<int> c;
	for(int j=0; j<4; j++)
		c.pb(Cm7[j]);

	for(int i=0; i<12; i++)
	{
		if(check(c,p)) ps("",i,"m7");
		ctension(c,p,i,"m7");
		
		for(int j=0; j<4; j++)
			c[j]=(c[j]+1)%12;

	}
}

void create(vector<int> p)
{
	createC(p);
	createC7(p);
	createCM7(p);
	createCm(p);
	createCm7(p);
}

int main()
{
	int N,M;
	cin >> N;
	
	init();
	
	while(N--)
	{
		ans.clear();

		vector<int> n;
		cin >> M;
		while(M--)
		{
			string s;
			cin >> s;
			n.push_back(dic[s]);
		}
		
		create(n);

		if(ans.size()!=0)
		{
			for(int i=0; i<ans.size(); i++)
			{
				if(i!=0) cout << " ";
				cout << ans[i];
			}
			cout << endl;
		}
		else
		{
			cout << "UNKNOWN" << endl;
		}
	}
}
accuracy: 1
time_ms: 480
memory_kb: 936
final_rank: 8