fork(4) download
  1. //#include GRAPH
  2. //#define DEBUG //comment when you have to disable all debug macros.
  3. //#define LOCAL //uncomment for testing from local file
  4. #define NDEBUG //comment when all assert statements have to be disabled.
  5. #include <iostream>
  6. #include <cstring>
  7. #include <sstream>
  8. #include <fstream>
  9. #include <cstdlib>
  10. #include <cstdio>
  11. #include <cmath>
  12. #include <vector>
  13. #include <set>
  14. #include <map>
  15. #include <unordered_map>
  16. #include <bitset>
  17. #include <climits>
  18. #include <ctime>
  19. #include <algorithm>
  20. #include <functional>
  21. #include <stack>
  22. #include <queue>
  23. #include <list>
  24. #include <deque>
  25. #include <sys/time.h>
  26. #include <iomanip>
  27. #include <cstdarg>
  28. #include <utility> //std::pair
  29. #include <cassert>
  30. #define fd(i,a) for(i=1;i<=a;i++)
  31. #define fa(i,a,b) for(i=a;i<=b;i++)
  32. #define fs(i,a,b,c) for(i=a;i<=b;i+=c)
  33. #define tr(c,i) for(typeof(c.begin()) i = (c).begin(); i != (c).end(); i++)
  34. #define present(c,x) ((c).find(x) != (c).end())
  35. #define all(x) x.begin(), x.end()
  36. #define pb push_back
  37. #define log2(x) (log(x)/log(2))
  38. #define ARRAY_SIZE(arr) (1[&arr]-arr)
  39. #define lld long long int
  40. #define MOD 1000000007
  41. #define INF 1000000000
  42. #define gcd __gcd
  43. #define equals(a,b) (a.compareTo(b)==0) //for strings only
  44. using namespace std;
  45.  
  46. #ifdef DEBUG
  47. #define debug(args...) {dbg,args; cerr<<endl;}
  48. #else
  49. #define debug(args...) // Just strip off all debug tokens
  50. #endif
  51.  
  52. #ifdef GRAPH
  53. #include "drawGraph.cpp"
  54. #endif
  55.  
  56. struct debugger
  57. {
  58. template<typename T> debugger& operator , (const T& v)
  59. {
  60. cerr<<v<<" ";
  61. return *this;
  62. }
  63.  
  64. }dbg;
  65.  
  66. template<class T>
  67. inline void inputInt(T &n )
  68. {
  69. n=0;
  70.  
  71. T ch=getchar_unlocked();
  72. /*int sign=1;
  73.   while(( ch < '0' || ch > '9') && ch!='-')
  74.   ch=getchar_unlocked();
  75.   if(ch=='-')
  76.   {
  77.   sign=-1;
  78.   ch=getchar_unlocked();
  79.   }
  80.   while( ch >= '0' && ch <= '9' )
  81.   n = (n<<3)+(n<<1) + ch-'0', ch=getchar_unlocked();
  82.   n*=sign;*/
  83. while( ch < '0' || ch > '9')
  84. ch=getchar_unlocked();
  85. while( ch >= '0' && ch <= '9' )
  86. n = (n<<3)+(n<<1) + ch-'0', ch=getchar_unlocked();
  87. }
  88.  
  89. // inline void inputString(string &s)
  90. // {
  91. // char ch=getchar_unlocked();
  92. // while(ch<'a' || ch >'z')ch=getchar_unlocked();
  93. // s="";
  94. // while(ch>='a' && ch<='z')
  95. // s+=ch, ch=getchar_unlocked();
  96. // }
  97.  
  98. inline void inputString(string &s)
  99. {
  100. char ch=getchar_unlocked();
  101. s="";
  102. while(ch!='\n')
  103. s+=ch, ch=getchar_unlocked();
  104. }
  105.  
  106.  
  107. typedef struct{
  108. int operator()(const pair<int,int> &k) const{
  109. return (k.first*100000+k.second)%MOD;
  110. }
  111. }myHash;
  112.  
  113.  
  114.  
  115. string s1,s2;
  116. lld len1, len2,a,b,k;
  117. typedef unordered_map<pair<lld,lld>, lld, myHash> MyMap;
  118. MyMap mp, cost_map;
  119. lld arrX[20100001];
  120. lld arrY[20100001];
  121.  
  122. inline lld actualK(){
  123. return k/a+1;
  124. }
  125.  
  126.  
  127.  
  128. inline void mapInsert(MyMap &m, pair<lld,lld> p, lld value){
  129. m.insert(make_pair(p,value));
  130. }
  131.  
  132. inline lld numbering(){
  133. lld ma,mi,i,j,index=1, t= actualK();
  134. for(i=1;i<=len1;i++){
  135. ma = max((lld)1,i-t);
  136. mi = min(i+t,len2);
  137. for(j=ma;j<=mi;j++){
  138. arrX[index] = i;
  139. arrY[index] = j;
  140. mapInsert(mp,make_pair(i,j),index);
  141. index++;
  142. }
  143. }
  144. return index;
  145. }
  146.  
  147. inline void init(){
  148. mp.clear();
  149. cost_map.clear();
  150. lld i, t = actualK();
  151. for(i=0;i<=t;i++)mapInsert(cost_map, make_pair(i,0), i*a);
  152. for(i=0;i<=t;i++)mapInsert(cost_map, make_pair(0,i), i*a);
  153. }
  154.  
  155. inline lld cost(lld x, lld y){
  156. pair<lld, lld> p = make_pair(x,y);
  157. lld val;
  158. if(cost_map.find(p)!=cost_map.end())val = cost_map[p]; //present
  159. else if(a) val = (lld)INF;
  160. else val = 0;
  161. return val;
  162. }
  163.  
  164. inline pair<lld,lld> calculate_cost(lld index){
  165. lld i,x,y,l_cost,u_cost,m_cost;
  166. pair<lld,lld> ins,pop;
  167. for(i=1;i<index;i++){
  168.  
  169. x = arrX[i]; y = arrY[i];
  170. l_cost = cost(x,y-1)+a;
  171. u_cost = cost(x-1,y)+a;
  172. m_cost = cost(x-1,y-1)+b*(s1[x-1]!=s2[y-1]);
  173. mapInsert(cost_map, make_pair(x,y), min(l_cost, min(u_cost, m_cost)));
  174. //debug(x,y,l_cost,u_cost,m_cost,cost_map[make_pair(x,y)], s1[x-1], s2[y-1]);
  175. }
  176. return make_pair(arrX[index-1],arrY[index-1]);
  177. }
  178.  
  179. lld solve(){
  180. lld index,total_cost;
  181. init();
  182. index = numbering();
  183. pair<lld,lld> ans = calculate_cost(index);
  184. lld x = ans.first, y = ans.second;
  185. total_cost = cost(x,y)+a*(len2-y);
  186. //debug("sssss",x,y,total_cost,len2,k,cost(x,y),a);
  187. if(total_cost<=k)return total_cost;
  188. else return -1;
  189. }
  190.  
  191. int main()
  192. {
  193. #ifdef LOCAL
  194. freopen("input.in","r",stdin);
  195. #endif
  196. //std::ios_base::sync_with_stdio (false);
  197. lld t;
  198. inputInt(t);
  199.  
  200. while(t--){
  201. inputString(s1);
  202. inputString(s2);
  203. //cin>>s1>>s2;
  204. inputInt(a);
  205. inputInt(b);
  206. inputInt(k);
  207. len1 = s1.length();
  208. len2 = s2.length();
  209. if(len2<len1)
  210. {
  211. swap(len1,len2);
  212. swap(s1,s2);
  213. }
  214. if(a==0){
  215. printf("0\n");
  216. continue;
  217. }
  218. if(k==0 || (len2-len1)*a>k){
  219. printf("-1\n");
  220. continue;
  221. }
  222. printf("%lld\n",solve());
  223. }
  224. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
7
aaa
bbbb
0 0 100
abab
acac
1 1 100
baaaaa
aaaaab
1 100 100
aaaaaa
bbbbbb
100 100 0
abababa
bababaa
10 3 1000
a
b
1 2 10
a
b
0 3 10
compilation info
In file included from /usr/include/c++/4.8/unordered_map:35:0,
                 from prog.cpp:15:
/usr/include/c++/4.8/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
 #error This file requires compiler and library support for the \
  ^
prog.cpp:117:9: error: ‘unordered_map’ does not name a type
 typedef unordered_map<pair<lld,lld>, lld, myHash> MyMap;
         ^
prog.cpp:118:1: error: ‘MyMap’ does not name a type
 MyMap mp, cost_map;
 ^
prog.cpp:128:23: error: variable or field ‘mapInsert’ declared void
 inline void mapInsert(MyMap &m, pair<lld,lld> p, lld value){
                       ^
prog.cpp:128:23: error: ‘MyMap’ was not declared in this scope
prog.cpp:128:30: error: ‘m’ was not declared in this scope
 inline void mapInsert(MyMap &m, pair<lld,lld> p, lld value){
                              ^
prog.cpp:128:47: error: expected primary-expression before ‘p’
 inline void mapInsert(MyMap &m, pair<lld,lld> p, lld value){
                                               ^
prog.cpp:39:13: error: expected primary-expression before ‘long’
 #define lld long long int
             ^
prog.cpp:128:50: note: in expansion of macro ‘lld’
 inline void mapInsert(MyMap &m, pair<lld,lld> p, lld value){
                                                  ^
prog.cpp: In function ‘long long int numbering()’:
prog.cpp:140:23: error: ‘mp’ was not declared in this scope
             mapInsert(mp,make_pair(i,j),index);
                       ^
prog.cpp:140:46: error: ‘mapInsert’ was not declared in this scope
             mapInsert(mp,make_pair(i,j),index);
                                              ^
prog.cpp: In function ‘void init()’:
prog.cpp:148:5: error: ‘mp’ was not declared in this scope
     mp.clear();
     ^
prog.cpp:149:5: error: ‘cost_map’ was not declared in this scope
     cost_map.clear();
     ^
prog.cpp:151:61: error: ‘mapInsert’ was not declared in this scope
     for(i=0;i<=t;i++)mapInsert(cost_map, make_pair(i,0), i*a);
                                                             ^
prog.cpp:152:61: error: ‘mapInsert’ was not declared in this scope
     for(i=0;i<=t;i++)mapInsert(cost_map, make_pair(0,i), i*a);
                                                             ^
prog.cpp: In function ‘long long int cost(long long int, long long int)’:
prog.cpp:158:8: error: ‘cost_map’ was not declared in this scope
     if(cost_map.find(p)!=cost_map.end())val = cost_map[p];   //present
        ^
prog.cpp: In function ‘std::pair<long long int, long long int> calculate_cost(long long int)’:
prog.cpp:173:19: error: ‘cost_map’ was not declared in this scope
         mapInsert(cost_map, make_pair(x,y), min(l_cost, min(u_cost, m_cost)));
                   ^
prog.cpp:173:77: error: ‘mapInsert’ was not declared in this scope
         mapInsert(cost_map, make_pair(x,y), min(l_cost, min(u_cost, m_cost)));
                                                                             ^
stdout
Standard output is empty