关注
在做题的始终是在想将所有的输出结果保存了然后最后一次性输出,结果始终输出不了。现在就直接将输出改为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
相关推荐
点赞 评论 收藏
分享
05-07 09:53
未填写教育信息 嵌入式工程师 点赞 评论 收藏
分享
05-03 11:27
Johns Hopkins University 算法工程师 点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 聊聊这家公司值得去吗 #
234015次浏览 2187人参与
# kpi面有什么特征 #
32170次浏览 224人参与
# 你认为哪个岗位找工作最卷 #
12935次浏览 42人参与
# 职场人,说说你的烦心事 #
8584次浏览 71人参与
# 一人一个landing小技巧 #
79225次浏览 1125人参与
# 职场上哪些事情令人讨厌 #
16670次浏览 82人参与
# 秋招最大的收获是什么? #
33874次浏览 297人参与
# 小红书求职进展汇总 #
56112次浏览 485人参与
# 聊聊你的职场新体验 #
157472次浏览 1369人参与
# 机械制造岗投递时间线 #
22722次浏览 346人参与
# 职场吐槽大会 #
205434次浏览 1636人参与
# 研究所VS国企,该如何选 #
180619次浏览 1769人参与
# 为了找工作你投递了多少公司? #
9828次浏览 133人参与
# 大家每天通勤多久? #
42005次浏览 329人参与
# 通信硬件牛牛的实习日记 #
7167次浏览 65人参与
# 职场破防瞬间 #
234930次浏览 2125人参与
# 总结:哪家公司面试体验感最好 #
47637次浏览 338人参与
# tplink提前批进度交流 #
162738次浏览 1378人参与
# 找工作前vs找工作后的心路变化 #
9592次浏览 103人参与
# 担心入职之后被发现很菜怎么办 #
126374次浏览 754人参与