#include<bits/stdc++.h> using namespace std; template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string& s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #if 0 #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif string s,t; int n,m; void print(vector<vector<int> > vec){ for(vector<int> v : vec){ for(int x : v){ cout <<x <<" "; } cout <<"\n"; } cout <<"\n"; } vector<vector<int> > fill(int sx,int fx,int sy,int fy){ vector<vector<int> > dp(fx-sx+1,vector<int>(fy-sy+1,0)); for(int i=sx;i<fx;i++){ for(int j=sy;j<fy;j++){ if(s[i]==t[j]){ dp[i-sx+1][j-sy+1]=1+dp[i-sx][j-sy]; } else{ dp[i-sx+1][j-sy+1]=max(dp[i-sx][j-sy+1],dp[i-sx+1][j-sy]); } } } return dp; } void extend(int len){ vector<vector<int> > dp(len+1,vector<int>(m+1)); string subs=s.substr(n-len,len); debug(subs); for(int l=m-1;l>=0;l--){ int j=0; for(int i=l;i<m;i++){ while(j<len+1){ if(dp[j][i] > dp[j][i+1]){ break; } else if(j+1 < len+1 && subs[j]==t[i] && dp[j][i]+1 > dp[j+1][i+1]){ j++; break; } j++; } for(int k=j;k<len+1;k++){ dp[k][i+1]++; } } } print(dp); if(dp!=fill(n-len,n,0,m)){ cout <<"NIE"; exit(0); } } const int BASE = 1050; void add(int x, int which, vector<vector<int> > &fenwick){ ++x; while(x<=BASE) { fenwick[which][x]+=1; x+=(x&-x); } } int get(int x, int which, vector<vector<int> > &fenwick){ ++x; int res=0; while(x>0){ res+=fenwick[which][x]; x-=(x&-x); } return res; } int ans[1234567]; vector<pair<int,pair<int,int> > > questions[3123][3123]; void extend_fast(int len){ vector<vector<int> > fenwick(m+1,vector<int>(BASE+1,0)); string subs=s.substr(n-len,len); debug(subs); for(int l=m-1;l>=0;l--){ int j=0; for(int i=l;i<m;i++){ while(j<len+1){ if(get(j,i,fenwick) > get(j,i+1,fenwick) ){ break; } else if(j+1 < len+1 && subs[j]==t[i] && (get(j,i,fenwick)+1 > get(j+1,i+1,fenwick) ) ){ j++; break; } j++; } add(j,i+1,fenwick); } for(auto q : questions[n-len+1][l+1]){ int which=q.first; debug(which); int askx = q.second.first - (n-len); int asky = q.second.second; ans[which]=get(askx,asky,fenwick); } } /*for(int i=0;i<len+1;i++){ for(int j=0;j<=m;j++){ cout <<get(i,j,fenwick) <<" "; } cout <<"\n"; } cout <<"\n";*/ } int main(){ int q; cin >>n >>m >>q; cin >>s >>t; for(int num=0;num<q;num++){ int i,j,k,l; cin >>i >>j >>k >>l; cout <<fill(i-1,j,k-1,l).back().back() <<"\n"; } }
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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | #include<bits/stdc++.h> using namespace std; template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string& s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #if 0 #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif string s,t; int n,m; void print(vector<vector<int> > vec){ for(vector<int> v : vec){ for(int x : v){ cout <<x <<" "; } cout <<"\n"; } cout <<"\n"; } vector<vector<int> > fill(int sx,int fx,int sy,int fy){ vector<vector<int> > dp(fx-sx+1,vector<int>(fy-sy+1,0)); for(int i=sx;i<fx;i++){ for(int j=sy;j<fy;j++){ if(s[i]==t[j]){ dp[i-sx+1][j-sy+1]=1+dp[i-sx][j-sy]; } else{ dp[i-sx+1][j-sy+1]=max(dp[i-sx][j-sy+1],dp[i-sx+1][j-sy]); } } } return dp; } void extend(int len){ vector<vector<int> > dp(len+1,vector<int>(m+1)); string subs=s.substr(n-len,len); debug(subs); for(int l=m-1;l>=0;l--){ int j=0; for(int i=l;i<m;i++){ while(j<len+1){ if(dp[j][i] > dp[j][i+1]){ break; } else if(j+1 < len+1 && subs[j]==t[i] && dp[j][i]+1 > dp[j+1][i+1]){ j++; break; } j++; } for(int k=j;k<len+1;k++){ dp[k][i+1]++; } } } print(dp); if(dp!=fill(n-len,n,0,m)){ cout <<"NIE"; exit(0); } } const int BASE = 1050; void add(int x, int which, vector<vector<int> > &fenwick){ ++x; while(x<=BASE) { fenwick[which][x]+=1; x+=(x&-x); } } int get(int x, int which, vector<vector<int> > &fenwick){ ++x; int res=0; while(x>0){ res+=fenwick[which][x]; x-=(x&-x); } return res; } int ans[1234567]; vector<pair<int,pair<int,int> > > questions[3123][3123]; void extend_fast(int len){ vector<vector<int> > fenwick(m+1,vector<int>(BASE+1,0)); string subs=s.substr(n-len,len); debug(subs); for(int l=m-1;l>=0;l--){ int j=0; for(int i=l;i<m;i++){ while(j<len+1){ if(get(j,i,fenwick) > get(j,i+1,fenwick) ){ break; } else if(j+1 < len+1 && subs[j]==t[i] && (get(j,i,fenwick)+1 > get(j+1,i+1,fenwick) ) ){ j++; break; } j++; } add(j,i+1,fenwick); } for(auto q : questions[n-len+1][l+1]){ int which=q.first; debug(which); int askx = q.second.first - (n-len); int asky = q.second.second; ans[which]=get(askx,asky,fenwick); } } /*for(int i=0;i<len+1;i++){ for(int j=0;j<=m;j++){ cout <<get(i,j,fenwick) <<" "; } cout <<"\n"; } cout <<"\n";*/ } int main(){ int q; cin >>n >>m >>q; cin >>s >>t; for(int num=0;num<q;num++){ int i,j,k,l; cin >>i >>j >>k >>l; cout <<fill(i-1,j,k-1,l).back().back() <<"\n"; } } |