关注
在做题的始终是在想将所有的输出结果保存了然后最后一次性输出,结果始终输出不了。现在就直接将输出改为cout输出了。现在虽然本地可以AC了,但是就想问问大佬们,我对多case的处理是否正确。 #include<iostream>
#include<string>
#include<vector>
using namespace std;
bool isMatch(const string &dict, const string &example);
vector<int> getNext(const string &needle, int m, vector<int> next);
int main()
{
while (cin) //此处是否正确?
{
int N;
cin >> N;
vector<string> dict(N, "");
for (int i = 0;i<N;i++)
{
cin >> dict[i];
}
int M;
cin >> M;
vector<string> example(M, "");
for (int i = 0;i<M;i++)
{
cin >> example[i];
}
for (int i = 0;i<M;i++)
{
int count = 0;
for (int j = 0;j<N;j++)
{
if (isMatch(dict[j], example[i]))
{
count++;
}
}
cout << count << endl; //输出
}
}
return 0;
}
//KMP匹配算法
bool isMatch(const string &dict, const string &example)
{
int n = dict.size(), i = 0;
int m = example.size(), j = 0;
if (m>n) return false;
if (n == 0 || m == 0) return true;
vector<int> next(m);
next = getNext(example, m, next);
while (j<m&&i<n)
{
if ((0>j) || dict[i] == example[j])
{
i++;
j++;
}
else
{
j = next[j];
}
}
return j == m ? true : false;
}
vector<int> getNext(const string &needle, int m, vector<int> next)
{
int t = -1;
next[0] = -1;
int j = 0;
while (j<m - 1)
{
if (0>t || needle[j] == needle[t])
{
j++;
t++;
next[j] = (needle[j] != needle[t] ? t : next[t]);
}
else
t = next[t];
}
return next;
}
查看原帖
点赞 1
相关推荐
点赞 评论 收藏
分享
04-20 19:47
东华理工大学 Web前端 点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 26届春招投递记录 #
29263次浏览 211人参与
# 我与AI的日常 #
9790次浏览 136人参与
# 27届实习投递记录 #
108112次浏览 1060人参与
# 你是怎么和mt相处的? #
109256次浏览 567人参与
# 我的求职总结 #
508287次浏览 7046人参与
# 数字马力求职进展汇总 #
356931次浏览 2407人参与
# 工作压力大怎么缓解 #
169598次浏览 1382人参与
# 腾讯工作体验 #
645236次浏览 3906人参与
# 材料专业就业可以去哪些企业岗位 #
69000次浏览 396人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
168468次浏览 913人参与
# 我的租房踩坑经历 #
223001次浏览 1156人参与
# 同花顺工作体验 #
17191次浏览 27人参与
# 牛客租房专区 #
206945次浏览 2582人参与
# 你的房租占工资的比例是多少? #
101656次浏览 906人参与
# 滴!实习打卡 #
860355次浏览 6898人参与
# 嵌入式转岗的难度怎么样 #
141539次浏览 2842人参与
# 如果公司降薪,你会跳槽吗? #
168328次浏览 966人参与
# 产运销实习日记 #
107418次浏览 740人参与
# 摸鱼被leader发现了怎么办 #
206929次浏览 937人参与
# 你在职场上见过哪些“水货”同事 #
41537次浏览 175人参与

华为HUAWEI工作强度 1372人发布