异想天开

What's the true meaning of light, Could you tell me why

A Famous Music Composer

日期:2015-05-08 19:23:33
  
最后更新日期:2015-05-09 11:53:52
NYOJ
http://acm.nyist.net/JudgeOnline/problem.php?pid=25
描述
大意为总共12个不同的音,有些音有不同的符号,比如A#和Bb表示同一个音:
A ,A#=Bb ,B ,C,C#=Db,D,D#=Eb,E,F,F#=Gb,G,G#=Ab
等号表示一样的音,要求输出A#求另外一个音,若只有一个音则输出UNIQUE。
输出
样例输入
Ab minor
D# major
G minor
样例输出
Case 1: G# minor
Case 2: Eb major
Case 3: UNIQUE
解析:
每个音符最多有两个字符,可以用一个int存储,第一个字符存低两位,第二个字符存高两位,用该int作为hash。
[code lang="cpp"]
#include<cstdio>
#include<map>
#include<string>
#include<cstdlib>

using namespace std;

int gethash(const char *p)
{
int hash;
if(NULL==p){
return -1;
}
hash=p[0];
if (p[1]!='\0'){
hash += p[1]*100;
}
return hash;
}

struct note{
string _v; //符号
int buddy; //伙伴
};

int buildhash(map<int,struct note> &m){
const char *p[]={"A","A#","Bb","B","C","C#","Db","D","D#","Eb","E","F","F#","Gb","G","G#","Ab"};
const char *b[]={0,"Bb","A#", 0, 0, "Db" ,"C#", 0,"Eb","D#",0 ,0 ,"Gb", "F#", 0 ,"Ab","G#"};
int hash;
// printf("%d %d",sizeof(p),sizeof(b));
// exit(0);
for (int i=0; i<sizeof(p)/sizeof(p[0]); ++i){
hash = gethash(p[i]);
struct note t;
t._v=p[i];
t.buddy=gethash(b[i]);
m[hash]=t;
}
return 0;
}


int sovle()
{
const int LEN=64;
char note[LEN],tonality[LEN];
map<int,struct note> m;
buildhash(m);

int i=1;
while(scanf("%s %s",note,tonality)!=-1){
int id=gethash(note);
if (m[id].buddy>0){
printf("Case %d: %s %s",i,m[m[id].buddy]._v.c_str(),tonality);
} else {
printf("Case %d: UNIQUE",i);
}
i++;
}
return 0;
}

int main()
{
sovle();
return 0;
}
[/code]