Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- /*
- floor will be zero if 2^x > len
- therefore 2^x <= len (i.e. n)
- 2^x <= n
- there x <= 9
- there binary representation of x can set bit only in first 4 bits
- so when taking xor or two substring only last 4 bits can differ rest all
- have to be same
- we can brute force those last atmost 4 length substring and then maximize length
- to the left of these substring as long as s[i] == t[i] .. store this max length in dp
- */
- int MaxProfit(int N, string s, string t) {
- int ans = 0;
- int dp[N][N] = {};
- for(int i = 0; i < N; i++) {
- for(int j = 0; j < N; j++) {
- if(s[i] != t[j]) continue;
- if(i && j) dp[i][j] = 1 + dp[i-1][j-1];
- }
- }
- for(int i = 0; i < N; i++) {
- for(int j = 0; j < N; j++) {
- int val = 0;
- for(int k = 0; k < 4 && i+k < N && j + k < N; k++) {
- val *= 2;
- val += s[i+k] != t[j+k];
- int len = k+1 + (i && j ? dp[i-1][j-1] : 0);
- ans = max(ans, len / (1 << val));
- }
- }
- }
- return ans;
- }
- int main() {
- int t;
- cin >> t;
- while(t--) {
- int n;
- string s, t;
- cin >> n >> s >> t;
- cout << MaxProfit(n, s, t) << "\n";
- }
- }
Add Comment
Please, Sign In to add comment