-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlovoizKube.cpp
More file actions
96 lines (81 loc) · 1.64 KB
/
SlovoizKube.cpp
File metadata and controls
96 lines (81 loc) · 1.64 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
string word = "";
string cubes[12];
int countCubes;
vector<int> partialSolution[12];
int itr[12];
int len = 0;
void BuildPartialSolution(int number){
for(int i = 0; i < len; i++)
for(int j = 0; j < 6; j++)
if(word[i] == cubes[number][j]){
partialSolution[i].push_back(number);
break;
}
}
int GetNumberNextElement(int letter){
int count = partialSolution[letter].size();
bool noExist;
if(letter < 0)
return -1;
for(int i = itr[letter] + 1; i < count; i++){
noExist = true;
for(int j = 0; j < letter; j++)
if(partialSolution[letter][i] == partialSolution[j][itr[j]]){
noExist = false;
break;
}
if(noExist)
return i;
}
return -1;
}
bool Compute(int index){
if(index >= len)
return true;
if(index < 0)
return false;
itr[index] = GetNumberNextElement(index);
if(itr[index] == -1)
return Compute(index - 1);
return Compute(index + 1);
}
int main(){
ofstream Out("output.txt");
ifstream In("input.txt");
In >> countCubes;
In >> word;
len = word.length();
for(int i = 0; i < countCubes; i++){
In >> cubes[i];
BuildPartialSolution(i);
itr[i] = -1;
}
In.close();
/* for(int i = 0; i < len; i++){
for(int j = 0; j < partialSolution[i].size(); j++){
cout << partialSolution[i][j];
cout << ' ';
}
cout << "\n";
}*/
for(int i = 0; i < len; i++)
if(partialSolution[i].size() == 0){
Out << '0';
Out.close();
return 0;
}
if(Compute(0))
for(int i = 0; i < len; i++){
Out << partialSolution[i][itr[i]] + 1;
Out << ' ';
}
else
Out << '0';
Out.close();
}