-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathB.java
68 lines (62 loc) · 2.15 KB
/
B.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.io.PrintStream;
import java.util.*;
/**
* KickStart 2017 Round A Problem B: Patterns Overlap
* Check README.md for explanation.
*/
public class Main {
private String solve(Scanner scanner) {
String s=scanner.next(), t=scanner.next();
int l1=s.length(), l2=t.length();
boolean[][] dp=new boolean[l1+1][l2+1];
dp[0][0]=true;
for (int i=1;i<=l1;i++) {
if (s.charAt(i-1)=='*') dp[i][0]=true;
else break;
}
for (int j=1;j<=l2;j++) {
if (t.charAt(j-1)=='*') dp[0][j]=true;
else break;
}
for (int i=1;i<=l1;i++) {
for (int j=1;j<=l2;j++) {
if (s.charAt(i-1)=='*') {
int cnt=0;
for (int k=j;k>=0;k--) {
if (dp[i-1][k]) {
dp[i][j]=true;
break;
}
if (k>0 && t.charAt(k-1)!='*') cnt++;
if (cnt>4) break;
}
}
if (t.charAt(j-1)=='*') {
int cnt=0;
for (int k=i;k>=0;k--) {
if (dp[k][j-1]) {
dp[i][j]=true;
break;
}
if (k>0 && s.charAt(k-1)!='*') cnt++;
if (cnt>4) break;
}
}
if (s.charAt(i-1)==t.charAt(j-1) && dp[i-1][j-1])
dp[i][j]=true;
}
}
return dp[l1][l2]?"TRUE":"FALSE";
}
public static void main(String[] args) throws Exception {
System.setOut(new PrintStream("output.txt"));
Scanner scanner=new Scanner(System.in);
int times=scanner.nextInt();
long start=System.currentTimeMillis();
for (int t=1;t<=times;t++) {
System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner)));
}
long end=System.currentTimeMillis();
System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0));
}
}