关注
在做题的始终是在想将所有的输出结果保存了然后最后一次性输出,结果始终输出不了。现在就直接将输出改为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
相关推荐
点赞 评论 收藏
分享
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 我的实习日记 #
4136193次浏览 33137人参与
# 你认为小厂实习有用吗? #
151821次浏览 793人参与
# 第3届现代汽车Code Faster急速编程挑战赛 #
7899次浏览 329人参与
# 为了找工作你投递了多少公司? #
119961次浏览 762人参与
# 机械人的offer怎么选 #
296402次浏览 1285人参与
# 实习生的生存小技巧 #
41765次浏览 361人参与
# 实习返校后,你的精神状态是__? #
47350次浏览 170人参与
# 你最近因为什么迷茫? #
106428次浏览 975人参与
# 我的租房踩坑经历 #
229955次浏览 1285人参与
# 通信硬件薪资爆料 #
1349477次浏览 7307人参与
# 牛友的春节生活 #
134123次浏览 838人参与
# 如果能重来,就业or读研你选哪个? #
331986次浏览 2843人参与
# 工作不开心辞职是唯一出路吗 #
20685次浏览 63人参与
# 牛客AI体验站 #
32972次浏览 447人参与
# 为什么国企只招应届生 #
271825次浏览 1347人参与
# 顺丰求职进展汇总 #
91607次浏览 372人参与
# 你觉得什么岗位会被AI替代 #
68691次浏览 401人参与
# 机械人求职现状 #
45556次浏览 335人参与
# 求职遇到的搞笑事件 #
209310次浏览 1081人参与
# 你觉得机械有必要实习吗 #
92172次浏览 543人参与
# 体制内上岸心路历程 #
42207次浏览 245人参与