首先列出词典中不超过100000条不同的魔咒词条,每条格式为: [魔咒] 对应功能 其中“魔咒”和“对应功能”分别为长度不超过20和80的字符串,字符串中保证不包含字符“[”和“]”,且“]”和后面的字符串之间有且仅有一个空格。词典最后一行以“@END@”结束,这一行不属于词典中的词条。 词典之后的一行包含正整数N(<=1000),随后是N个测试用例。每个测试用例占一行,或者给出“[魔咒]”,或者给出“对应功能”。
每个测试用例的输出占一行,输出魔咒对应的功能,或者功能对应的魔咒。如果魔咒不在词典中,就输出“what?”
[expelliarmus] the disarming charm [rictusempra] send a jet of silver light to hit the enemy [tarantallegra] control the movement of one's legs [serpensortia] shoot a snake out of the end of one's wand [lumos] light the wand [obliviate] the memory charm [expecto patronum] send a Patronus to the dementors [accio] the summoning charm @END@ 4 [lumos] the summoning charm [arha] take me to the sky
light the wand accio what? what?
#include<iostream> #include<cstdio> #include<string> #include<map> using namespace std; int main(){ int d,l; string s; map<string,string> mp; while(getline(cin,s)){ if(s=="@END@") break; d=s.find("]");//定位指定字符的位置,为了将字符串分割 l=s.length(); string m=s.substr(0,d+1);//利用substr提取相关字符串 string n=s.substr(d+2,l); mp[m]=n;//将分开的两段字符串分别映射成键和值 mp[n]=m; } int n; scanf("%d",&n); getchar(); string t; while(n--&&getline(cin,s)){ if(mp[s]==""){//如果没有找到相关的映射 cout<<"what?"<<endl; }else{ t=mp[s]; if(t.find("[")!=-1){//如果找到的字符串里含有指定字符,就利用substr去除 cout<<t.substr(1,(t.length()-2))<<endl; }else{//如果没有就直接输出 cout<<t<<endl; } } } return 0; }
#include <stdio.h> #include <string.h> char str[100005][120]; char dst[120]; char temp[120]; int main() { int cnt=0; while(fgets(str[cnt],200,stdin)) { if(strcmp(str[cnt],"@END@\n")==0) break; cnt++; } int i,j,k,n; // while(scanf("%d",&n)!=EOF) // { scanf("%d",&n); getchar(); for(i=0;i<n;i++) { fgets(dst,120,stdin); int len=strlen(dst); dst[len-1]='\0'; int flag=0; for(j=0;j<cnt;j++) { char* p=str[j];//指向魔咒词典的每一个条目 int length=strlen(p); if(strchr(dst,'[')&&strchr(dst,']')&&strstr(p,dst))//输入魔咒名,查询魔咒功能 { char* q=strchr(p,']');//找到']'出现的位置 strncpy(temp,p,q-p+1);//将词典条目当中的魔咒名拷贝到字符数组temp当中 temp[q-p+1]='\0'; if(strcmp(temp,dst)==0)//确保待查询的魔咒名与词典中的魔咒名完全一致(防止子串) { flag=1; printf("%s",p+len); break; } } else//输入魔咒功能,查询魔咒名 { if(strstr(p,dst)) { char* q=strchr(p,']'); strcpy(temp,q+2); temp[p+length-q-3]='\0';//将魔咒词典当中的魔咒功能拷贝到字符数组temp当中 if(strcmp(temp,dst)==0) { flag=1; for(k=1;k<q-p;k++) { printf("%c",p[k]); } printf("\n"); break; } } } } if(flag==0) printf("what?\n"); } // } return 0; }
dkey, dvalue = dict(), dict()
a = input()
while a != "@END@":
key, val = a.split("] ")
dkey[key[1:]] = val
dvalue[val] = key[1:]
a = input()
for i in range(int(input())):
a = input()
if a.startswith("["):
if a.strip("[").strip("]") in dkey.keys():
print(dkey[a.strip("[").strip("]")])
else:
print("what?")
else:
if a in dvalue.keys():
print(dvalue[a])
else:
print("what?")
try: while 1: a, b = {}, {} while 1: Item = raw_input() if Item == '@END@': break Index = Item.index(']') Word, Function = Item[:Index + 1], Item[Index + 2:] a[Word], b[Function] = Function, Word for i in xrange(input()): Query = raw_input() if Query in a: print a[Query] elif Query in b: print b[Query][1: -1] else: print 'what?' except: pass
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { char name[23]; char content[81]; } Enchant; int trn(char *str1,char *str2) { int i; for(i=1; str1[i]!=']'; i++) str2[i-1]=str1[i]; str2[i-1]='\0'; return i; } int cmp1(const void *a,const void *b) { return strcmp((*(Enchant**)a)->name,(*(Enchant**)b)->name); } int cmp2(const void *a,const void *b) { return strcmp((*(Enchant**)a)->content,(*(Enchant**)b)->content); } int bsrc1(Enchant **enchant,char *str,int n) { int low=0,high=n-1; while(low<=high) { int t=strcmp(str,enchant[(low+high)/2]->name); if(t==0) return (low+high)/2; if(t>0) low=(low+high)/2+1; else high=(low+high)/2-1; } return -1; } int bsrc2(Enchant **enchant,char *str,int n) { int low=0,high=n-1; while(low<=high) { int t=strcmp(str,enchant[(low+high)/2]->content); if(t==0) return (low+high)/2; if(t>0) low=(low+high)/2+1; else high=(low+high)/2-1; } return -1; } int main() { Enchant *enchant1[100000]; Enchant *enchant2[100000]; char str[104]; int count=0; scanf("%[^\n]%*c",str); while(str[0]!='@') { enchant1[count]=(Enchant*)malloc(sizeof(Enchant)); int i=trn(str,enchant1[count]->name); int j; for(i+=2,j=0; str[i]!='\0'; i++,j++) enchant1[count]->content[j]=str[i]; enchant1[count]->content[j]='\0'; enchant2[count]=enchant1[count]; count++; scanf("%[^\n]%*c",str); } qsort(enchant1,count,sizeof(Enchant*),cmp1); qsort(enchant2,count,sizeof(Enchant*),cmp2); int n; scanf("%d%*c",&n); for(int i=0; i<n; i++) { scanf("%[^\n]%*c",str); if(str[0]=='[') { trn(str,str); int t=bsrc1(enchant1,str,count); if(t==-1) printf("what?\n"); else printf("%s\n",enchant1[t]->content); } else { int t=bsrc2(enchant2,str,count); if(t==-1) printf("what?\n"); else printf("%s\n",enchant2[t]->name); } } return 0; }
#include<iostream> (720)#include<cstdio> #include<map> (747)#include<string> using namespace std; map<string,string>dictionary; int main(){ string str;//循环 while(getline(cin,str)){ if(str == "@END@"){ break; } int pos = str.find("]"); string key = str.substr(0,pos+1); string value = str.substr(pos + 2); dictionary[key] = value; dictionary[value] = key;//双向映射; } int n; scanf("%d",&n); getchar(); while(n--){ string key; getline(cin,key); string answer = dictionary[key];// new key; if(answer == ""){ answer = "what?"; }else if(answer[0] == '['){ answer = answer.substr(1,answer.size() - 2); } cout<<answer<<endl; } return 0; }
#include <cstdio> #include <iostream> #include <map> #include <string> using namespace std; map<string, string>dictionary; int main() { string str; while (getline(cin, str) && str != "@END@") { int pos = str.find(']'); //分界点 string key = str.substr(0, pos + 1); //魔咒 string value = str.substr(pos + 2); //功能 dictionary[key] = value; //魔咒映射功能 dictionary[value] = key; //功能映射魔咒(双向映射) } int n; cin >> n; getchar(); //吃掉回车 while (n--) { string key; getline(cin, key); string answer = dictionary[key]; cout << (answer == "" ? "what?" : //魔咒或功能找不到 answer[0] == '[' ? answer.substr(1, answer.size() - 2) : //魔咒需要删除方括号 answer) << endl; } return 0; }
#include <cstdio> #include <iostream> #include <map> using namespace std; // 魔咒字典——[魔咒] 对应功能 int main() { map<string, string> dict; while (true) { string str; getline(cin, str); if (str == "@END@") { break; } int pos = str.find("]"); // substr(start, len) string word = str.substr(0, pos+1); // 中括号中的内容 string info = str.substr(pos+2); // 中括号中的内容 // word、info 既是关键字又是内容 dict[word] = info; dict[info] = word; } int n; scanf("%d", &n); getchar();// 将换行符吃掉,不然会被当做查找内容 for (int i = 0; i < n; i++) { string traget; getline(cin, traget);// 必须要getline!!!,因为字符串内包含空格!!! if (dict.find(traget) != dict.end()) {// 存在某个魔咒 if (traget[0] == '[') {// 若是[开头,则直接输出info cout << dict[traget] << endl; }else { // 将“[]”去掉后再输出 string m = dict[traget]; m = m.substr(1, m.size()-2); cout << m << endl; } } else {// 不存在 cout << "what?" << endl; } } return 0; } //[expelliarmus] the disarming charm //[rictusempra] send a jet of silver light to hit the enemy //[tarantallegra] control the movement of one's legs //[serpensortia] shoot a snake out of the end of one's wand //[lumos] light the wand //[obliviate] the memory charm //[expecto patronum] send a Patronus to the dementors //[accio] the summoning charm //@END@ //4 //[lumos] //the summoning charm //[arha] //take me to the sky
#include<iostream> #include<string> #include<cstring> #include<unordered_map> using namespace std; int main() { string str; unordered_map<string,string> d; while(getline(cin,str)&&str!="@END@"){ int pos = str.find("]"); string key = str.substr(0,pos+1); string value = str.substr(pos+2); d[key] = value; d[value] = key.substr(1,key.size()-2); } int n; cin>>n; cin.get(); while(n--){ string key; getline(cin,key); string ans = d[key]; if(ans == "") cout<<"what?"<<endl; else cout<<ans<<endl; } return 0; }
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); List<String> myList = new ArrayList<String>(); while (scanner.hasNext()) { while (scanner.hasNext()) { String str = scanner.nextLine(); if (str.equals("@END@")) { break; } myList.add(str); } int n = scanner.nextInt(); scanner.nextLine(); //消耗换行符 for (int i = 0; i < n; i++) { String string = scanner.nextLine(); if (string.charAt(0) == '[') { //输入的是魔咒 int count = 0; for (int j = 0; j < myList.size(); j++) { String[] strs = myList.get(j).split("]"); if (string.substring(0,string.length()-1).equals(strs[0])) { System.out.println(strs[1].substring(1)); count++; break; } } if (count == 0) { //没有匹配 System.out.println("what?"); } }else { //输入的是功能 int count = 0; for (int j = 0; j < myList.size(); j++) { String[] strs = myList.get(j).split("]"); if (string.equals(strs[1].substring(1))) { System.out.println(strs[0].substring(1)); count++; break; } } if (count == 0) { //没有匹配 System.out.println("what?"); } } } } } }
#include "map" #include "iostream" #include "cstring" #include <algorithm> #include <cstdio> #include <string> using namespace std; const int N = 100010; map<string, string > cidian ; int n; int main() { string plus; while (getline(cin, plus)) { string mozhou, func; if (plus == "@END@") break;; int p2 = plus.find(']'); mozhou = plus.substr(0,p2+1); func = plus.substr(p2 + 2); cidian[mozhou] = func; cidian[func] = mozhou; } scanf("%d", &n); getchar(); while (n--) { string test; getline(cin, test); string ans=cidian[test]; if (ans=="") { ans="what?"; } else if(ans[0]=='['){ ans= ans.substr(1,ans.size()-2); } cout << ans <<endl; } return 0; }
#include "cstdio" #include "map" #include "string" using namespace std; int main(){ map<string,string> dict; //构建词典 while (true){ char line[200]; fgets(line,200,stdin); string linestr = line; linestr.pop_back();//去掉末位'\0' if (linestr=="@END@"){ break; } string word = linestr.substr(0,linestr.find(']')+1); string info = linestr.substr(linestr.find(']')+2); dict[word]=info; dict[info]=word; } int N; scanf("%d",&N); getchar(); for (int i = 0; i < N; ++i) { char line[200]; fgets(line,200,stdin); string string1 = line; string1.pop_back(); if (dict.find(string1) != dict.end()){ if (string1[0]=='['){ printf("%s\n",dict[string1].c_str()); }else{ printf("%s\n",dict[string1].substr(1,dict[string1].size()-2).c_str()); } } else{ printf("what?\n"); } } }
import java.util.*; //有个换行 注意下 // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Map<String,String> map1 = new HashMap<>(); Map<String,String> map2 = new HashMap<>(); Scanner in = new Scanner(System.in); String curse = in.nextLine(); while(!curse.equals("@END@")){ int pos = curse.indexOf(']'); map1.put(curse.substring(0,pos + 1),curse.substring(pos + 2)); map2.put(curse.substring(pos + 2),curse.substring(1,pos)); curse = in.nextLine(); } int n = in.nextInt(); String blank = in.nextLine();//这里有个换行 for(int i = 0;i<n;i++){ String word = in.nextLine(); String res = map1.get(word); if(res == null){ res = map2.get(word); } System.out.println(res == null ? "What?" : res); } } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); HashMap<String, String> hashMap1 = new HashMap<>(); //<咒语,功能> HashMap<String, String> hashMap2 = new HashMap<>(); //<功能,咒语> while (in.hasNextLine()) { String str = in.nextLine(); if (!str.equals("@END@")) { //未到结尾处 String[] split = str.split("]"); String key = split[0].substring(1);//获取键值 String value = split[1].substring(1);//获取参数值 hashMap1.put(key, value); hashMap2.put(value, key); } else {//到达结尾处 int n = in.nextInt();//需要输出的个数 in.nextLine(); //如果要在nextInt()后面再输入一个字符串,就需要加上一个nextLine()用来消除内存中的回车 for (int i = 0; i < n; i++) { String s = in.nextLine(); // System.out.println(s); if (s.contains("[")) {//输出功能 String s_key = s.substring(1, s.length() - 1); if (hashMap1.get(s_key) != null) System.out.println(hashMap1.get(s_key)); else System.out.println("what?"); } else {//输出咒语 if (hashMap2.get(s) != null) System.out.println(hashMap2.get(s)); else System.out.println("what?"); } } break; } } } }