File tree Expand file tree Collapse file tree 2 files changed +141
-0
lines changed
src/com/blankj/study/temp Expand file tree Collapse file tree 2 files changed +141
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .blankj .study .temp ;
2
+
3
+ import java .util .Scanner ;
4
+
5
+ public class Test01 {
6
+
7
+
8
+ public static void main (String [] args ) {
9
+ Scanner sc = new Scanner (System .in );
10
+ int n = sc .nextInt ();
11
+ int m = sc .nextInt ();
12
+ String [] str1 = new String [n ];
13
+ String [] str2 = new String [m ];
14
+ for (int i = 0 ; i < n ; i ++) {
15
+ str1 [i ] = sc .next ();
16
+ }
17
+ for (int i = 0 ; i < m ; i ++) {
18
+ str2 [i ] = sc .next ();
19
+ }
20
+ //str2[0]
21
+
22
+ for (int k = 0 ; k < m ; k ++) {
23
+ //str2去str1中找
24
+ int [] res = new int [n ];
25
+ for (int i = 0 ; i < n ; i ++) {
26
+ res [i ] = 1 ;
27
+ }
28
+ int sum = 0 ;
29
+ for (int i = 0 ; i < n ; i ++) {
30
+ if (str1 [i ].charAt (0 ) == '*' ) {
31
+ int end = str2 [k ].length () - 1 ;
32
+ for (int j = str1 [i ].length () - 1 ; j >= 1 ; j --) {
33
+ if (str1 [i ].charAt (j ) == str2 [k ].charAt (end )) {
34
+ end --;
35
+ continue ;
36
+ } else {
37
+ res [i ] = 0 ;
38
+ break ;
39
+ }
40
+ }
41
+ } else if (str1 [i ].charAt (str1 [i ].length () - 1 ) == '*' ) {
42
+ int start = 0 ;
43
+ for (int j = 0 ; j <= str1 [i ].length () - 2 ; j ++) {
44
+ if (str1 [i ].charAt (j ) == str2 [k ].charAt (start )) {
45
+ start ++;
46
+ continue ;
47
+ } else {
48
+ res [i ] = 0 ;
49
+ break ;
50
+ }
51
+ }
52
+ } else {
53
+ for (int j = 0 ; j < str1 [i ].length (); j ++) {
54
+ if (str1 [i ].charAt (j ) == str2 [k ].charAt (j )) {
55
+ continue ;
56
+ } else {
57
+ res [i ] = 0 ;
58
+ break ;
59
+ }
60
+ }
61
+ }
62
+ }
63
+ for (int i = 0 ; i < n ; i ++) {
64
+ sum += res [i ];
65
+ }
66
+ if (sum == 0 ) {
67
+ System .out .print (0 + " " );
68
+ } else {
69
+ System .out .print (1 + " " );
70
+ }
71
+
72
+ }
73
+
74
+ }
75
+ }
Original file line number Diff line number Diff line change
1
+ package com .blankj .study .temp ;
2
+
3
+ import java .util .Arrays ;
4
+ import java .util .Scanner ;
5
+
6
+ /**
7
+ * 获取字符串数组公共前缀
8
+ */
9
+ public class Test02 {
10
+
11
+ public static void main (String [] args ) {
12
+ Scanner scanner = new Scanner (System .in );
13
+ String input = scanner .nextLine ();
14
+ String [] temp = input .split (" " );
15
+
16
+
17
+ String ans = duplicateFront (temp );
18
+
19
+ System .out .println (ans );
20
+ }
21
+
22
+ public static String duplicateFront (String [] strs ) {
23
+ if (!check (strs )) {
24
+ return "" ;
25
+ }
26
+
27
+ Arrays .sort (strs );
28
+
29
+ StringBuilder sb = new StringBuilder ();
30
+
31
+ int len = strs .length ;
32
+
33
+ int m = strs [0 ].length ();
34
+ int n = strs [len - 1 ].length ();
35
+ int l = Math .min (m , n );
36
+
37
+ for (int i = 0 ; i < l ; i ++) {
38
+ if (strs [0 ].charAt (i ) == strs [len - 1 ].charAt (i )) {
39
+ sb .append (strs [0 ].charAt (i ));
40
+ } else {
41
+ break ;
42
+ }
43
+ }
44
+
45
+
46
+ return sb .toString ();
47
+
48
+
49
+ }
50
+
51
+ private static boolean check (String [] strs ) {
52
+ boolean flag = false ;
53
+ if (strs != null ) {
54
+ for (String str : strs ) {
55
+ if (str != null && str .length () != 0 ) {
56
+ return true ;
57
+ } else {
58
+ flag = false ;
59
+ break ;
60
+ }
61
+ }
62
+ }
63
+ return flag ;
64
+ }
65
+
66
+ }
You can’t perform that action at this time.
0 commit comments