英文:
Counting equal elements characters in java
问题
我正在Codeforces上做一些练习并学习Java。我遇到了解决方案结果的差异。当我在控制台中测试它们时,它们按预期工作,但只有一个在Codeforces的判题中有效。
有这段代码(在Java中可以通过测试):https://codeforces.com/contest/1005/submission/208626855
还有这段代码,不能通过测试6,可能还有其他测试:https://codeforces.com/contest/1005/submission/208624411
测试6的输入如下:
-字符串a:“aaaaaaaaaaaaa”...重复399991次
-字符串b:“”
输出:我的代码(在Codeforces中为5),而真正的输出是399991。
我不知道发生了什么差异,也不知道除了这里之外还可以在哪里提问。感谢您的帮助。
英文:
I'm doing some exercises on codeforces and learning java. And i came across this deviation in solution results. When i test them in my console they work as intended but only one works on codeforces judge.
There's this code(that can pass the tests, in java): https://codeforces.com/contest/1005/submission/208626855
And this one, that can't pass test 6 and probably many others: https://codeforces.com/contest/1005/submission/208624411
Test 6 is something like, input:
-string a:"aaaaaaaaaaaaa"... 399991 times
-string b:""
Output: mine(in codeforces is 5) and real one is 399991.
I don't know what differences are happening and don't know where can I ask this besides here, thank you for the help.
答案1
得分: 1
The task is basically to find the common tail of two strings and print the number of characters of both strings that are not part of the common tail.
This is the code we are talking about.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String a=br.readLine();
String b=br.readLine();
char [] arrA = a.toCharArray();
char [] arrB = b.toCharArray();
long count =0;
if(arrA.length > arrB.length) count = arrA.length - arrB.length;
else count = arrB.length - arrA.length;
count
is the difference in length of both strings. The beginning of the longer string cannot be part of the common tail.
But this for
loop is wrong:
for(int i=0; i < Math.min(arrA.length, arrB.length); i++) {
if(arrA[arrA.length-1-i] != arrB[arrB.length-1-i]) count = count + 2;
}
System.out.println(count);
You iterate backwards and idle as long as the characters of both strings match. However, once they differ, you must add them, regardless of whether they match again or not. Consider
ABA
ACA
This version will print 2 instead of the correct 4, because it counts B
and C
, but not the first two A
. You could fix it like this
boolean commonTail = true;
for(int i=0; i < Math.min(arrA.length, arrB.length); i++) {
if(arrA[arrA.length-1-i] != arrB[arrB.length-1-i])
commonTail = false;
if(!commonTail)
count = count + 2;
}
System.out.println(count);
英文:
The task is basically to find the common tail of two strings and print the number of characters of both strings that are not part of the common tail.
This is the code we are talking about.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String a=br.readLine();
String b=br.readLine();
char [] arrA = a.toCharArray();
char [] arrB = b.toCharArray();
long count =0;
if(arrA.length>arrB.length) count = arrA.length-arrB.length;
else count = arrB.length-arrA.length;
count
is the difference in length of both strings. The beginning of the longer string cannot be part of the common tail.
But this for
loop is wrong:
for(int i=0;i<Math.min(arrA.length,arrB.length);i++) {
if(arrA[arrA.length-1-i]!=arrB[arrB.length-1-i]) count=count+2;
}
System.out.println(count);
You iterate backwards and idle as long as the characters of both strings match. However, once they differ, you must add them, regardless of whether they match again or not. Consider
ABA
ACA
This version will print 2 instead of the correct 4, because it counts B
and C
, but not the first two A
. You could fix it like this
boolean commonTail = true;
for(int i=0;i<Math.min(arrA.length,arrB.length);i++) {
if(arrA[arrA.length-1-i]!=arrB[arrB.length-1-i])
commonTail = false;
if(!commonTail)
count=count+2;
}
System.out.println(count);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论