解题思路
map映射和字符串处理的考查
注意:
魔咒中也有可能含有空格,必须按照括号来判别魔咒的结束位置
#include<bits/stdc++.h>
using namespace std;
int main(){
string str, s, t, a, b;
int d;
unordered_map<string,string> mp;
while(getline(cin,s)){
if(s=="@END@") break;
d = s.find("]");
a = s.substr(0,d+1); //子串提取出来
b = s.substr(d+2,s.size());
mp[a] = b;
mp[b] = a;
}
int m;
cin>>m;
getchar();
while(m--){
getline(cin,str);
if(mp[str]==""){
cout<<"what?"<<endl;
}else{
t = mp[str];
if(t.find("[") != -1){ //功能映射魔咒
cout<<t.substr(1,t.size()-2)<<endl;
}else{
cout<<t<<endl; //是魔咒映射功能
}
}
}
return 0;
}
substr()函数
只写起始位置默认提取至末尾
#include<bits/stdc++.h>
using namespace std;
int main(){
string s = "hello world i!";
string str = s.substr(5); //只写起始位置默认提取至末尾
cout<<str<<endl;
return 0;
}