-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
116 lines (92 loc) · 3.06 KB
/
main.cpp
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int times [1001]={0}; //ftable
vector<long long int> exp;
string dictionary [1001];
long long int sentenceHash[1000];
long long int dictionaryHash[1000];
int modula = 1000000007;
long long int rehash(int hash1, int hash2){
long long int h2 = sentenceHash[hash2];
long long int difh = exp[hash2-hash1]*sentenceHash[hash1]%modula;
long long int result = h2-difh;
result=result%modula;
result=result+modula;
result=result%modula;
return result;
}
int fun(int sentenceLength, int start, int dicSize) {
int count = 0;
if (times[start] == 0) {
if (start == sentenceLength) {
times[start] = 1;
return 1;
}
for (int i = 0; i < dicSize; i++) {
if (start == 0) {
if ( dictionaryHash[i] == sentenceHash[dictionary[i].length() - 1]) {
count =count + fun(sentenceLength, start + dictionary[i].length(), dicSize);
count=count%modula;
}
} else {
if ((dictionaryHash[i]%modula) == rehash(start - 1, start + dictionary[i].length() - 1)%modula) {
count =count + fun(sentenceLength, start + dictionary[i].length(), dicSize);
count=count%modula;
}
}
}
times[start]=count;
}else{
count=times[start];
}
return count;
}
int main(int argc, char* argv[]) {
if (argc != 3) {
cout << "Run the code with the following command: ./project5 [input_file] [output_file]" << endl;
return 1;
}
ifstream infile(argv[1]);
ofstream outfile;
outfile.open(argv[2]);
string line;
infile >> line;
vector<char> sentence; ///sonradan ekledim
copy(line.begin(), line.end(), back_inserter(sentence)); //sonradan ekledim
int num;
infile >> num;
for (int i = 0; i < num; ++i) {
infile >> dictionary[i];
}
int sentenceLength = line.length();
exp.push_back(1);
for(int i=1;i<sentenceLength;i++){
exp.push_back(exp[i-1]*31%modula);
}
sentenceHash[0]=sentence[0];
for (int i = 1; i < sentenceLength; i++) {
// sentenceHash[i] = ((line.at(i))+sentenceHash[i-1]*exp[1])%modula;
sentenceHash[i] = ((sentence[i])+sentenceHash[i-1]*exp[1])%modula;
}
for (int i = 0; i < num; i++) {
int wordLength = dictionary[i].length();
for (int j = 0; j < wordLength; ++j) {
// if (j == 0)
// dictionaryHash[i] = dictionary[i].at(0);
// else
// dictionaryHash[i] = (((dictionaryHash[i]) * exp[1]) + dictionary[i].at(j)) % modula;
if (j > 0)
dictionaryHash[i] = (((dictionaryHash[i]) * exp[1]) + dictionary[i].at(j)) % modula;
else
dictionaryHash[i] = dictionary[i].at(0);
}
}
int result = fun(sentenceLength, 0, num);
result+=modula;
result%=modula;
outfile<<result<<endl;
outfile.close();
return 0;
}