关注
楼主,基于字典树的第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;
}
}
查看原帖
点赞 评论
相关推荐
点赞 评论 收藏
分享
01-06 04:55
重庆邮电大学 Java 点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 面试被问第一学历差时该怎么回答 #
97978次浏览 615人参与
# 你见过最离谱的招聘要求是什么? #
151869次浏览 951人参与
# 水滴春招 #
37855次浏览 598人参与
# 你的房租占工资的比例是多少? #
18103次浏览 223人参与
# 你想留在一线还是回老家? #
17573次浏览 284人参与
# 听劝,这个简历怎么改 #
24957次浏览 320人参与
# 顺丰求职进展汇总 #
41878次浏览 252人参与
# 互联网行业现在还值得去吗 #
2693次浏览 23人参与
# 嵌入式岗知多少 #
24297次浏览 289人参与
# 2025,我想...... #
28494次浏览 309人参与
# 机械人的offer怎么选 #
119685次浏览 629人参与
# 大学最后一个寒假,我想…… #
18604次浏览 205人参与
# 面试被问“你的缺点是什么?”怎么答 #
15519次浏览 286人参与
# 第一份工作应该选高薪还是热爱? #
11566次浏览 122人参与
# 机械人,你在招聘流程中的企业有哪些? #
21788次浏览 205人参与
# 入职第四天,心情怎么样 #
13644次浏览 110人参与
# 招银网络科技工作体验 #
16045次浏览 81人参与
# 牛友投递互助,不漏校招机会 #
233127次浏览 3245人参与
# 0offer是寒冬太冷还是我太菜 #
1044556次浏览 8694人参与
# 租房找室友 #
8875次浏览 57人参与
# 大城市找工作会更容易吗 #
5795次浏览 31人参与