File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import java .util .*;
2+
3+ public class Main {
4+ static int n ;
5+ static int k ;
6+ static String [] words ;
7+ static boolean [] visited = new boolean [26 ];
8+
9+ static int answer = -1 ;
10+
11+ public static void main (String [] args ) {
12+
13+ Scanner sc = new Scanner (System .in );
14+ n = sc .nextInt ();
15+ k = sc .nextInt ();
16+ words = new String [n ];
17+
18+ String start = "anta" ;
19+ String end = "tica" ;
20+ for (int i = 0 ; i < 4 ; i ++) {
21+ visited [start .charAt (i ) - 'a' ] = true ;
22+ visited [end .charAt (i ) - 'a' ] = true ;
23+ }
24+
25+ for (int i = 0 ; i < n ; i ++) {
26+ words [i ] = sc .next ();
27+ }
28+
29+ solve ();
30+
31+ }
32+
33+ public static void solve () {
34+ if (k < 5 ) {
35+ System .out .println (0 );
36+ return ;
37+ }
38+
39+ if (k == 26 ) {
40+ System .out .println (n );
41+ return ;
42+ }
43+
44+ dfs (0 , 0 );
45+
46+ System .out .println (answer );
47+
48+ }
49+
50+ static void dfs (int idx , int count ) {
51+
52+ if (count == k - 5 ) {
53+ int countWord = 0 ;
54+ for (int i = 0 ; i < words .length ; i ++) {
55+ for (int j = 0 ; j < words [i ].length (); j ++) {
56+ if (!visited [words [i ].charAt (j ) - 'a' ])
57+ break ;
58+ if (j == words [i ].length () - 1 )
59+ countWord ++;
60+ }
61+ }
62+ answer = Math .max (answer , countWord );
63+ return ;
64+ }
65+
66+ for (int i = idx ; i < 26 ; i ++) {
67+ if (visited [i ])
68+ continue ;
69+
70+ visited [i ] = true ;
71+ dfs (i + 1 , count + 1 );
72+ visited [i ] = false ;
73+ }
74+ }
75+
76+ }
You can’t perform that action at this time.
0 commit comments