Kattis问题,纹理分析.Java



我在Kattis上调试纹理分析时遇到问题。我能够完成我能想象到的每个测试用例,但最后一个测试用例失败了。有人知道最后一个测试用例失败的原因吗?

链接到kattis问题

public static void main(String[] args) throws Exception {
BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter dc = new BufferedWriter(new OutputStreamWriter(System.out));
int numOfLines = 1;
for (;;) {
String inputLine = sc.readLine();
if (inputLine.equals("END"))
break;
else if (!inputLine.contains(".")) {
dc.write(numOfLines++ + " EVENn");
} else {
//Next two lines creates an array out of the characters in a single line
String[] tempArray = inputLine.split("");
ArrayList<String> charArray = new ArrayList<String>(Arrays.asList(tempArray));
//Next block of code finds all elements where an asterix has been found and puts them in the allIndexes array
//If the code is even, the elements will for example be on 0, 2, 4, 6
//If the code is uneven, the elements will for example be on 0, 1, 5, 8
String str = "*";
List<Integer> allIndexes =
IntStream.range(0, charArray.size()).boxed()
.filter(j -> charArray.get(j).equals(str))
.collect(Collectors.toList());
ArrayList<Integer> duplicateList = new ArrayList<>();
//For-loop, is used to normalize numbers
//An even array of 0,2,4,6 turns into 2,2,2,2
//An uneven array of 0,2,6,8 turns into 2,4,2,2
//This means we can easily see where the uneven number is
for (int j = 0; j < allIndexes.size(); j++) {
if(j < allIndexes.size() - 1)
duplicateList.add(allIndexes.get(j + 1) - allIndexes.get(j));
}
Boolean isArrayUneven = false;
//Here we check for uneven numbers in the array
//If one is found then isArrayUneven turns true.
for (int i = 1; i < duplicateList.size();i++) {
if (duplicateList.get(0) != duplicateList.get(i)) {
isArrayUneven = true;
}
}
if (isArrayUneven ==true ) {
dc.write(numOfLines++ + " NOT EVEN" + "n");
}
else{
dc.write(numOfLines++ + " EVEN" + "n");
}
}
}
dc.close();
sc.close();
}

虽然这个解决方案对我来说有点过于设计,但我编写了一个被Kattis接受的工作解决方案,然后用各种测试的脚本将其与您的解决方案进行了比较,没有发现任何差异。有一点可能是Java版本行为或某种平台问题,但我无法想象会发生什么。以下是我的解决方案和测试脚本,以防有帮助:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class TextureAnalysis {
public static void main(String[] args) throws java.io.IOException {
var sc = new BufferedReader(new InputStreamReader(System.in));
var dc = new BufferedWriter(new OutputStreamWriter(System.out));
for (int i = 1;; i++) {
String s = sc.readLine();

if (s.equals("END")) {
break;
}
var a = s.split("\*", -1);
boolean allSameLength = true;
for (int j = 2; j < a.length - 1; j++) {
if (a[j].length() != a[1].length()) {
allSameLength = false;
break;
}
}
dc.write(i + (allSameLength ? " EVENn" : " NOT EVENn"));
}
dc.close();
sc.close();
}
}

测试脚本:

import random
import sys
from subprocess import Popen, PIPE

def start(cmd):
return Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)

def read(process):
return process.stdout.read().decode("utf-8")

def write(process, s):
process.stdin.write(f"{s}n".encode("utf-8"))
process.stdin.flush()

def terminate(process):
process.stdin.close()
process.terminate()
process.wait(timeout=0.2)

def main():
# even tests
for i in range(100):
good = start(["java", "TextureAnalysis"])
bad = start(["java", "TextureAnalysisBad"])
for j in range(100):
chunk = "*".join(["." * i for _ in range(j)])
test = "*" + chunk + "*"
write(good, test)
write(bad, test)
write(good, "END")
write(bad, "END")
g = read(good)
b = read(bad)
if g != b:
print("FAIL:", test)
sys.exit(0)
# random tests, mostly uneven
for _ in range(1000000):
good = start(["java", "TextureAnalysis"])
bad = start(["java", "TextureAnalysisBad"])

for j in range(100):
chunk = "".join(
[random.choice(".*") for _ in range(random.randint(10, 150))]
)
test = "*" + chunk + "*"
write(good, test)
write(bad, test)

write(good, "END")
write(bad, "END")
g = read(good)
b = read(bad)

if g != b:
print("FAIL:", test)
sys.exit(0)

if __name__ == "__main__":
main()

(子流程代码记入本帖(

最新更新