触宝第一题,那个判断子串的(java版)


import java.util.Scanner;

public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {

int n = Integer.parseInt(sc.nextLine());
String[] arr = new String[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextLine();
}
int m = Integer.parseInt(sc.nextLine());
for (int i = 0; i < m; i++) {
String str = sc.nextLine();
int count = 0;
for (int j = 0; j < n; j++) {
if (arr[j].contains(str)) {
++count;
}
}

System.out.println(count);
}
}
}
}

提交就AC,不知道为啥
至于第二题,强烈怀疑题有问题。我怎么看都是31.83
接受反驳,希望我错了~

#Java工程师#
全部评论
没有两道AC感觉 完全没机会面试
点赞 回复 分享
发布于 2017-09-05 21:51
好像不是这么简单的输入输出吧。
点赞 回复 分享
发布于 2017-09-08 10:03
兄弟,多交流
点赞 回复 分享
发布于 2017-09-06 11:15
在做题的始终是在想将所有的输出结果保存了然后最后一次性输出,结果始终输出不了。现在就直接将输出改为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; }
点赞 回复 分享
发布于 2017-09-05 22:15
第二题,90分钟时间也整不出,想的是用斜率做,结果数学方面知识忘记太多,根本整不出啊
点赞 回复 分享
发布于 2017-09-05 21:55
我也是这么AC的,不知道大家为啥都不行。。。
点赞 回复 分享
发布于 2017-09-05 21:37
第一次发帖,代码区和文字去反了……
点赞 回复 分享
发布于 2017-09-05 21:24

相关推荐

争当牛马还争不上
码农索隆:1.把简历改哈 2.猛投,狠投 3.把基础打牢 这样你在有机会的时候,才能抓住
点赞 评论 收藏
分享
frutiger:逆天,我家就安阳的,这hr咋能说3k的,你送外卖不比这工资高得多?还说大厂来的6k,打发叫花子的呢?这hr是怎么做到说昧良心的话的
找工作时遇到的神仙HR
点赞 评论 收藏
分享
05-30 12:03
山西大学 C++
offer来了我跪着...:不是骗子,等到测评那一步就知道为啥这么高工资了
点赞 评论 收藏
分享
评论
点赞
2
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务