关注
在做题的始终是在想将所有的输出结果保存了然后最后一次性输出,结果始终输出不了。现在就直接将输出改为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
相关推荐

点赞 评论 收藏
分享
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 考研对你找工作产生了哪些影响? #
5089次浏览 60人参与
# 打杂的实习你会去吗? #
108661次浏览 949人参与
# 聊聊这家公司值得去吗 #
239005次浏览 2236人参与
# 机械只有读研才有出路吗? #
19872次浏览 228人参与
# 你认为哪个岗位找工作最卷 #
16524次浏览 66人参与
# 面试被问第一学历差时该怎么回答 #
130724次浏览 823人参与
# 远程面试的尴尬瞬间 #
100987次浏览 828人参与
# 硬件人绝对不能踩的坑 #
61325次浏览 736人参与
# kpi面有什么特征 #
35694次浏览 264人参与
# 工作中哪个瞬间让你想离职 #
24195次浏览 166人参与
# 如何缓解入职前的焦虑 #
187287次浏览 1319人参与
# 你有哪些缓解焦虑的方法? #
3416次浏览 131人参与
# 职场人,说说你的烦心事 #
9034次浏览 83人参与
# 实习生应该准时下班吗 #
223513次浏览 1398人参与
# 秋招最大的收获是什么? #
34214次浏览 302人参与
# 职场上哪些事情令人讨厌 #
16899次浏览 86人参与
# 为了找工作你投递了多少公司? #
12066次浏览 164人参与
# 你今年的平均薪资是多少? #
126746次浏览 661人参与
# 运营/市场营销人的秋招现状 #
17268次浏览 189人参与
# 担心入职之后被发现很菜怎么办 #
126924次浏览 760人参与