字符串处理的题目;
学习了一下string类的一些用法;
这个代码花的时间很长,其实可以更加优化;
代码:
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 string dict[114]= {"he","h","li","be","b","c","n","o","f","ne" 5 ,"na","mg","al","si","p","s","cl","ar","k","ca" 6 ,"sc","ti","v","cr","mn","fe","co","ni","cu","zn" 7 ,"ga","ge","as","se","br","kr","rb","sr","y","zr" 8 ,"nb","mo","tc","ru","rh","pd","ag","cd","in","sn" 9 ,"sb","te","i","xe","cs","ba","hf","ta","w","re" 10 ,"os","ir","pt","au","hg","tl","pb","bi","po","at" 11 ,"rn","fr","ra","rf","db","sg","bh","hs","mt","ds" 12 ,"rg","cn","fl","lv","la","ce","pr","nd","pm","sm" 13 ,"eu","gd","tb","dy","ho","er","tm","yb","lu","ac" 14 ,"th","pa","u","np","pu","am","cm","bk","cf","es" 15 ,"fm","md","no","lr" 16 }; 17 18 bool go(string &s,int k) 19 { 20 if(k==s.length()) 21 return true; 22 for(int i=0; i<114; i++) 23 if(s.substr(k,dict[i].length())==dict[i] && go(s,k+dict[i].length())) 24 return true; 25 return false; 26 } 27 28 int main() 29 { 30 int tt; 31 cin>>tt; 32 string s; 33 while(tt--) 34 { 35 cin>>s; 36 cout<<(go(s,0)?"YES":"NO")<<endl; 37 } 38 return 0; 39 }