关注
在做题的始终是在想将所有的输出结果保存了然后最后一次性输出,结果始终输出不了。现在就直接将输出改为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
相关推荐
牛客热帖
更多
正在热议
更多
# 你觉得大几开始实习最合适? #
4402次浏览 48人参与
# 金融银行求职进展汇总 #
329629次浏览 1810人参与
# 厦门银行科技岗值不值得投 #
11599次浏览 293人参与
# 大厂实习和小厂实习最大的区别是什么? #
12121次浏览 83人参与
# 你都用vibe coding做过什么? #
1969次浏览 60人参与
# 如果人生可以debug你会改哪一行? #
2370次浏览 54人参与
# 招商银行数字金融训练营 #
44591次浏览 671人参与
# AI Coding实战技巧 #
1557次浏览 41人参与
# Vibe Coding 会干掉初级岗位吗? #
4155次浏览 80人参与
# 你见过哪些招聘隐形歧视? #
2580次浏览 33人参与
# 做完笔试后你收到面试了吗? #
3771次浏览 50人参与
# 面试被问到不会的问题,你怎么应对? #
4061次浏览 35人参与
# 牛友の3月总结 #
8145次浏览 79人参与
# 你现在一天AI几次? #
1254次浏览 41人参与
# 七猫笔试 #
5721次浏览 37人参与
# 选完offer后,你后悔学本专业吗 #
66613次浏览 262人参与
# 实习学到最有价值的工作习惯 #
66841次浏览 541人参与
# 哪些公司真双非友好? #
71396次浏览 305人参与
# 最难的技术面是哪家公司? #
71724次浏览 1063人参与
# 你认为小厂实习有用吗? #
133427次浏览 720人参与
# 快手工作体验 #
321703次浏览 2932人参与
# 找AI工作可以去哪些公司? #
24793次浏览 1062人参与

