首先列出词典中不超过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<map>
#include<string>
using namespace std;
int main() {
string s;
map<string,string>mym;//双向映射
while (true) {
getline(cin,s,'\n'); //循环键入魔咒
if("@END@"==s){
break;
}
int pos=s.find("]");//可能魔咒中有空格
mym.insert({s.substr(0,pos+1),s.substr(pos+2)});
mym.insert({s.substr(pos+2),s.substr(0,pos+1)});
//将魔咒与功能同时插入map中方便查找
}
int n;
scanf("%d",&n);
getchar();//吞空格
map<string,string>::iterator it;
for(int i=0;i<n;i++){
string temp;
getline(cin,temp,'\n');
it=mym.find(temp);
if(it!=mym.end()){
if(it->second[0]=='['){
it->second=it->second.substr(1,it->second.size()-2);
}
printf("%s\n",it->second.c_str());
}else{
printf("what?\n");
}
}
return 0;
} #include <iostream>
#include <type_traits>
using namespace std;
#include <map>
#include "vector"
int stringtoint(string str) {
int res = 0;
for (int i = 0; i < str.size(); i++) {
res = res * 10 + str[i] - '0';
}
return res;
}
int main() {
int m;//测试用例个数
string str;
map<string, string>my_map;
vector<string>mz;
while (getline(cin, str)) {
string mozhou;
string gongneng;
int left = -1, right = -2;
if (str[0] >= '0' && str[0] <= '9') {
m = stringtoint(str);
string input;
for (int i = 0; i < m; i++) {
getline(cin, input);
if (input[0] == '['){
input = input.substr(1, input.size() - 2); // 去掉方括号
if (my_map.count(input)) // 如果找到魔咒
cout << my_map[input] << endl;
else
cout << "what?" << endl; // 找不到
}else
{ // 匹配功能
bool found = false; // 标记是否找到对应的魔咒
for (auto & it : my_map) {
if (it.second == input) { // 如果找到了对应的魔咒
cout << it.first << endl;
found = true; // 设置标记为真
break; // 找到后跳出循环
}
}
if (!found)// 如果没有找到
cout << "what?" << endl;
}
}
}else {
for (int i = 0; i < str.size(); i++) {
if (str[i] == '[')
left = i;
else if (str[i] == ']')
right = i;
}
if (left < right) {
mozhou = str.substr(left + 1, right - 1);
gongneng = str.substr(right + 2);
my_map[mozhou] = gongneng;
}
}
}
} #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;
}