1 #define _for(i,a,b) for(int i = (a);i < (b);i ++) 2 class Solution 3 { 4 public: 5 vector<string> spellchecker(vector<string>& wordlist, vector<string>& queries) 6 { 7 vector<string> rnt; 8 set<string> ws; 9 map<string,int> wxs; 10 map<string,int> wys; 11 12 _for(i,0,wordlist.size()) 13 ws.insert(wordlist[i]); 14 _for(i,0,wordlist.size()) 15 { 16 string tmp = wordlist[i]; 17 _for(j,0,tmp.size()) 18 tmp[j] = tolower(tmp[j]); 19 if(!wxs.count(tmp)) 20 wxs.insert({tmp,i}); 21 } 22 _for(i,0,wordlist.size()) 23 { 24 string tmp = wordlist[i]; 25 _for(j,0,tmp.size()) 26 { 27 tmp[j] = tolower(tmp[j]); 28 if(tmp[j]==‘a‘||tmp[j]==‘e‘||tmp[j]==‘i‘ 29 ||tmp[j]==‘o‘||tmp[j]==‘u‘) 30 tmp[j] = ‘*‘; 31 } 32 if(!wys.count(tmp)) 33 wys.insert({tmp,i}); 34 } 35 36 _for(i,0,queries.size()) 37 { 38 if(ws.count(queries[i])) 39 {rnt.push_back(queries[i]);continue;} 40 41 string tmp = queries[i]; 42 _for(j,0,tmp.size()) 43 tmp[j] = tolower(tmp[j]); 44 auto pp = wxs.find(tmp); 45 if(pp!=wxs.end()) 46 { 47 rnt.push_back(wordlist[pp->second]); 48 continue; 49 } 50 51 _for(j,0,tmp.size()) 52 if(tmp[j]==‘a‘||tmp[j]==‘e‘||tmp[j]==‘i‘ 53 ||tmp[j]==‘o‘||tmp[j]==‘u‘) 54 tmp[j]=‘*‘; 55 auto pp2 = wys.find(tmp); 56 if(pp2!=wys.end()) 57 { 58 rnt.push_back(wordlist[pp2->second]); 59 continue; 60 } 61 rnt.push_back(""); 62 } 63 return rnt; 64 } 65 };
原文地址:https://www.cnblogs.com/Asurudo/p/10199702.html
时间: 2024-10-03 02:30:08