关注
在做题的始终是在想将所有的输出结果保存了然后最后一次性输出,结果始终输出不了。现在就直接将输出改为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
相关推荐
点赞 评论 收藏
分享
03-10 11:23
门头沟学院 Java 点赞 评论 收藏
分享
查看3道真题和解析 点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 这个offer值得去吗? #
1833次浏览 27人参与
# 你实习是赚钱了还是亏钱了? #
116549次浏览 628人参与
# 联宝杯大学生创新大赛,你的技术值得产业级答案 #
42711次浏览 496人参与
# 你会因为行情,降低找工作标准吗? #
8393次浏览 86人参与
# 面试官拷打AI项目都会问什么? #
1774次浏览 87人参与
# 如果春招能重来,我会___ #
4222次浏览 45人参与
# 想做Agent可以做哪些岗位? #
2257次浏览 27人参与
# 你觉得最好用的AI编程工具是_ #
885次浏览 24人参与
# 除了线上,还能去哪些地方投简历 #
3039次浏览 33人参与
# 你和你的mentor相处模式是__ #
5699次浏览 45人参与
# 实习第一天,你在干什么 #
3582次浏览 26人参与
# 实习想申请秋招offer,能不能argue薪资 #
253552次浏览 1308人参与
# 如何排解工作中的焦虑 #
325934次浏览 2798人参与
# mt对你说过最有启发的一句话 #
115131次浏览 872人参与
# 暑假倒计时,你都干了些啥? #
58838次浏览 313人参与
# 美的求职进展汇总 #
374307次浏览 2079人参与
# 你的mentor是什么样的人? #
61592次浏览 796人参与
# 大疆求职进展汇总 #
703210次浏览 4353人参与
# 金融银行面经 #
109040次浏览 557人参与
# 0offer互助地 #
776844次浏览 4782人参与
# 0经验如何找实习? #
90767次浏览 944人参与

