关注
在做题的始终是在想将所有的输出结果保存了然后最后一次性输出,结果始终输出不了。现在就直接将输出改为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
相关推荐
点赞 评论 收藏
分享
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 对2025年忏悔 #
2989次浏览 88人参与
# 新年的第一句祝福 #
50719次浏览 374人参与
# 实习没人带,苟住还是跑路? #
9861次浏览 227人参与
# 运营来爆料 #
72397次浏览 454人参与
# 元旦假期你打算怎么过 #
6451次浏览 159人参与
# 腾讯音乐求职进展汇总 #
145508次浏览 1039人参与
# 春招前还要继续实习吗? #
2924次浏览 49人参与
# 面试官问过你最刁钻的问题是什么? #
7393次浏览 88人参与
# 领导秒批的请假话术 #
30282次浏览 120人参与
# 一人说一家双休的公司 #
5769次浏览 87人参与
# 大家实习都在做什么? #
7637次浏览 83人参与
# 我们是不是被“优绩主义”绑架了? #
8116次浏览 276人参与
# 阿里求职进展汇总 #
443863次浏览 3919人参与
# 电网笔面经互助 #
56856次浏览 470人参与
# 如何提高实习转正率? #
73001次浏览 463人参与
# 腾讯工作体验 #
548162次浏览 3658人参与
# 实习教会我的事 #
47621次浏览 356人参与
# 面试常问题系列 #
262121次浏览 4672人参与
# 牛客2025仙途报告 #
34241次浏览 467人参与
# 国企还是互联网,你怎么选? #
190996次浏览 1476人参与