楼主,基于字典树的第21行会出现越界问题,比如case: zyyhappy zyy 不同长度的解决方案,用标识isEnd来控制,贴一下我的代码,如果有问题,还望各位指正。 #include <iostream> #include <list> #include <vector> #include <map> #include <string> #include <algorithm> using namespace std; class Tire { public: Tire *next[26]; bool isEnd; Tire() { for(int i = 0 ; i < 26 ;i++) next[i] = nullptr; isEnd = false; } }; bool findSS(Tire *root, string s) { for(int i=0 ; i < s.size() ;i++) { int index = s[i] - 'a'; if(root->next[index] == nullptr) return false; root = root->next[index]; } return root->isEnd; } void addTire(Tire* node, string word) { for(int i = 0 ; i < word.size() ;i++) { int index = word[i] - 'a'; if(node->next[index] == nullptr) node->next[index] = new Tire(); node = node->next[index]; } node->isEnd = true; } int main() { int t,n; string s; cin >> t; while(t--) { cin >> n; Tire *root = new Tire(); vector<string> v; bool sign = false; for(int i = 0 ; i < n ; i++) { cin >> s; string new_s = s+s; for(int k = 0 ; k < s.size()+1 ;k++) { string find_s = new_s.substr(k,s.size()); if(findSS(root,find_s)) { sign = true; break; } reverse(find_***egin(), find_s.end()); if(findSS(root, find_s)) { sign = true; break; } } addTire(root, s); } if(sign) cout << "Yeah" << endl; else cout << "Sad" << endl; } }
点赞 评论

相关推荐

02-18 17:30
腾讯_TEG_技术
多刷**&nbsp;背八股&nbsp;刷面经&nbsp;项目话术准备好&nbsp;不会差的!!!后台看到好多小伙伴们都出现其中一个环节的错误,,,可惜了抓紧机会吧&nbsp;有的是hc&nbsp;但缺的就是稍微用心的人
野猪不是猪🐗:多刷星星,背八股背话术,真的能过你们?对一个个没实习过的学生狂问场景题设计题和底层深挖,别以为我不知道一边说缺人还一边各种kpi面
点赞 评论 收藏
分享
点赞 评论 收藏
分享
牛客网
牛客企业服务