-
Notifications
You must be signed in to change notification settings - Fork 3
/
ADAMATCH.cpp
133 lines (110 loc) · 3.01 KB
/
ADAMATCH.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double ld;
#define F first
#define S second
template<class T> ostream& operator<<(ostream &os, vector<T> V) {
os << "[ ";
for(auto v : V) os << v << " ";
os << "]";
return os;
}
template<class T> ostream& operator<<(ostream &os, set<T> S){
os << "{ ";
for(auto s:S) os<<s<<" ";
return os<<"}";
}
template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {
return os << "(" << P.first << "," << P.second << ")";
}
template<class L, class R> ostream& operator<<(ostream &os, map<L,R> M) {
os << "{ ";
for(auto m:M) os<<"("<<m.F<<":"<<m.S<<") ";
return os<<"}";
}
// Kevin Mathew T
// Birla Institute of Technology, Mesra
// GitHub - https://github.com/KevinMathewT
// CodeForces - https://codeforces.com/profile/KevinMathew
// CodeChef - https://www.codechef.com/users/KevinMathew
// HackerRank - https://www.hackerrank.com/KevinMathew?
using cd = complex<double>;
const double PI = acos(-1);
void fft(vector<cd> & a, bool invert) {
ll n = a.size();
for (ll i = 1, j = 0; i < n; i++) {
ll bit = n >> 1;
for (; j & bit; bit >>= 1)
j ^= bit;
j ^= bit;
if (i < j)
swap(a[i], a[j]);
}
for (ll len = 2; len <= n; len <<= 1) {
double ang = 2 * PI / len * (invert ? -1 : 1);
cd wlen(cos(ang), sin(ang));
for (ll i = 0; i < n; i += len) {
cd w(1);
for (ll j = 0; j < len / 2; j++) {
cd u = a[i+j], v = a[i+j+len/2] * w;
a[i+j] = u + v;
a[i+j+len/2] = u - v;
w *= wlen;
}
}
}
if (invert) {
for (cd & x : a)
x /= n;
}
}
vector<ll> multiply(vector<ll> const& a, vector<ll> const& b) {
vector<cd> fa(a.begin(), a.end()), fb(b.begin(), b.end());
ll n = 1;
while (n < a.size() + b.size())
n <<= 1;
fa.resize(n);
fb.resize(n);
fft(fa, false);
fft(fb, false);
for (ll i = 0; i < n; i++)
fa[i] *= fb[i];
fft(fa, true);
vector<ll> result(n);
for (ll i = 0; i < n; i++)
result[i] = round(fa[i].real());
return result;
}
string s1, s2;
vector<ll> v1, v2;
ll tot[900010];
void solve(){
fill(tot, tot + 900010, 0);
cin >> s1 >> s2;
char ch[] = {'A', 'G', 'T', 'C'};
for(ll i=0;i<4;i++){
v1 = vector<ll>(s1.size(), 0);
v2 = vector<ll>(s2.size(), 0);
for(ll j=0;j<s1.size();j++)
if(s1[j] == ch[i])
v1[j] = 1;
for(ll j=0;j<s2.size();j++)
if(s2[j] == ch[i])
v2[s2.size() - j - 1] = 1;
vector<ll> v3 = multiply(v1, v2);
for(ll i=s2.size();i<s1.size() + s2.size();i++)
tot[i-s2.size()] += v3[i];
}
cout << s2.size() - (*max_element(tot, tot + s1.size() - s2.size())) << "\n";
}
int main()
{
// freopen("input.txt", "r", stdin); //Comment
// freopen("output.txt", "w", stdout); //this out.
ios::sync_with_stdio(false); //Not
cin.tie(NULL); //this.
cout.tie(0); //or this.
solve();
return 0;
}