-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlanguageDevice.java
More file actions
67 lines (63 loc) · 2.08 KB
/
languageDevice.java
File metadata and controls
67 lines (63 loc) · 2.08 KB
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
import java.util.*;
import java.io.*;
class main{
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter wr = new PrintWriter(System.out);
int N = Integer.parseInt(br.readLine().trim());
String[] LanguageDevice = new String[26];
for(int i_LanguageDevice=0; i_LanguageDevice<26; i_LanguageDevice++)
{
LanguageDevice[i_LanguageDevice] = br.readLine();
}
int out_ = CountPossibleStrings(LanguageDevice, N);
System.out.println(out_);
wr.close();
br.close();
}
static int subSet(String s,String res , int idx , String [] arr){
if(idx==s.length()){
if(res.length()>0 && isValid(res , arr)) return 1;
return 0;
}
int count=0;
char ch = s.charAt(idx);
count+=subSet(s, res+ch, idx+1,arr);
return count + subSet(s, res, idx+1,arr);
}
static Set<String> set = new HashSet<>();
static boolean isValid(String s , String [] arr){
if(set.contains(s)){
return false;
}
if(s.length()==1) return true;
for(int i=1;i<s.length();i++){
char ch1=s.charAt(i-1);
char ch2=s.charAt(i);
if(arr[ch1-65].charAt(ch2-65)!='1'){
return false;
}
}
set.add(s);
System.out.println(s);
return true;
}
static int CountPossibleStrings(String[] str, int N){
int count=0;
for(int i=0;i<str.length;i++){
StringBuilder sb = new StringBuilder();
for(int j=0;j<str[i].length();j++){
if(str[i].charAt(j)-'0'==1){
char ch = (char)(j+65);
sb.append(ch);
}
}
if(sb.length()>0){
int temp=subSet(sb.toString(), "", 0, str);
// System.out.println(sb+"\t"+temp);
count=(count+temp)%1000000007;
}
}
return count;
}
}