P3864 [USACO1.2]命名那个数字 Name That Number
分析:此题本来有个极复杂的代码,后来看了某位大佬的stl解法,感觉极为简便自己写了一个,
采用*lower_bound指针操作学到了(其功能是查询区间中匹配字符串)
还有substr(n)b表示截取第n个字符后的字符串,(substr的一种用法)
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<iomanip>
using namespace std;
string s[5000],k[3*3*3*3*3*3*3*3*3*3*3*3+1];
string num,key[10]={"","","ABC","DEF","GHI","JKL","MNO","PRS","TUV","WXY"};
int cnt,flag;
void dfs(string num,string result)
{
if(num.empty())
{
k[cnt++]=result;
return ;
}
for(int i=0;i<3;i++)dfs(num.substr(1),result+key[num[0]-'0'][i]);
return ;
}
int main()
{
ios::sync_with_stdio(false);
cin>>num;
for(int i=1;i<=4617;i++)
{
cin>>s[i];
}
dfs(num,"");
for(int i=0;i<cnt;i++)
{
if(*lower_bound(s+1,s+4618,k[i])==k[i])
{
cout<<*lower_bound(s+1,s+4618,k[i])<<endl;
flag=1;
}
}
if(!flag)cout<<"NONE"<<endl;
return 0;
}