关注
在做题的始终是在想将所有的输出结果保存了然后最后一次性输出,结果始终输出不了。现在就直接将输出改为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
相关推荐
牛客热帖
更多
- 1... 春招极限三选一!4.2W
- 2... 我的春招结束了1.5W
- 3... 我的求职总结 | 致那个一边崩溃一边投简历的自己,赢现金奖励!1.4W
- 4... 我进字节她考编,明知要分手但确没人敢开口1.3W
- 5... 非科班+本科目前正在做AI工程师,说说我这3年。。。1.2W
- 6... 26届,五月,0 offer,0保底,0面试,收拾收拾准备送外卖5399
- 7... 被妈妈说的感觉自己好没用啊😭5238
- 8... 海力士总市值突破9000亿美元,国内能赌哪些公司?4829
- 9... 27腾讯云智暑期面经4757
- 10... 实习一周天天给+1买咖啡买饭,不给钱!!4272
正在热议
更多
# AI让海力士市值突破9000亿美元 #
7442次浏览 66人参与
# 跟HR说什么能被秒回? #
45814次浏览 322人参与
# 在爱玛,骑向未来 #
47910次浏览 458人参与
# 我的求职总结 #
469197次浏览 6659人参与
# 你有哪些缓解焦虑的方法? #
62499次浏览 916人参与
# 通信硬件公司爆料 #
219048次浏览 559人参与
# 你后悔自己读研吗? #
65139次浏览 330人参与
# 拼多多工作体验 #
60303次浏览 422人参与
# 百度工作体验 #
337690次浏览 2296人参与
# 得物app工作体验 #
66346次浏览 113人参与
# 职场新人体验 #
194343次浏览 1268人参与
# 什么专业适合考公 #
70909次浏览 394人参与
# 牛油的搬砖plog #
203926次浏览 1315人参与
# 百度求职进展汇总 #
734123次浏览 6462人参与
# AI Coding实战技巧 #
30939次浏览 387人参与
# 这些公司卡简历很严格 #
106265次浏览 456人参与
# 机械笔面试考察这些知识点 #
20537次浏览 156人参与
# 米哈游求职进展汇总 #
693628次浏览 3368人参与
# 工作后,你落下了哪些病根 #
42942次浏览 302人参与
# 为了求职,我做过的疯狂伪装 #
88866次浏览 781人参与
# 硬件人求职现状 #
538717次浏览 4838人参与
查看22道真题和解析