fork(1) download
  1. //============================================================================
  2. // Name :
  3. // Author : Atul Kumar Gupta
  4. // Description :
  5. // Status : AC
  6. //============================================================================
  7.  
  8. #include <bits/stdc++.h>
  9.  
  10. using namespace std;
  11.  
  12. #define mod 1000000007
  13. #define dg(x) cout << '>' << #x << ':' << x << endl;
  14. #define sz(c) (int) c.size()
  15. #define all(c) c.begin(), c.end()
  16. #define tr(container,it) for(__typeof(container.begin()) it = container.begin();it != container.end(); it++)
  17. #define present(s,x) s.find(x) != s.end()
  18. #define cpresent(c,x) find(all(c),x) != c.end()
  19. #define pb push_back
  20. #define mp make_pair
  21. #define ff first
  22. #define ss second
  23. #define rep(i,n) for(int i=0;i<n;i++)
  24. #define dep(i,n) for(int i=n-1;i>=0;i--)
  25. #define repn(i,a,b) for(int i=a; i<b;i++)
  26. #define ini(n) scanf("%d",&n)
  27. #define ind(n,m) scanf("%d %d",&n,&m);
  28. #define inl(n) scanf("%l64d",&n)
  29. #define ins(n) scanf("%s",n);
  30. #define opi(n) printf("%d",n)
  31. #define opl(n) printf("%l64d",n)
  32. #define ops(n) printf("%s",n)
  33. #define opn printf("\n")
  34. #define opsp printf(" ");
  35. #define opa(n) cout<<n
  36.  
  37. //#define TRACE
  38.  
  39. #ifdef TRACE
  40. #define trace1(x) cerr << #x << ": " << x << endl;
  41. #define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
  42. #define trace3(x, y, z) cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
  43. #define trace4(a, b, c, d) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
  44. #define trace5(a, b, c, d, e) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
  45. #define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
  46.  
  47. #else
  48.  
  49. #define trace1(x)
  50. #define trace2(x, y)
  51. #define trace3(x, y, z)
  52. #define trace4(a, b, c, d)
  53. #define trace5(a, b, c, d, e)
  54. #define trace6(a, b, c, d, e, f)
  55.  
  56. #endif
  57.  
  58. typedef pair<int,int> pi;
  59. typedef vector<pi> vp;
  60. typedef vector<vp> vvp;
  61. typedef vector<int> vi;
  62. typedef unsigned long long int ull;
  63. typedef long long int ll;
  64. typedef vector<ll> vl;
  65. typedef vector< vi > vvi;
  66. typedef priority_queue<int> pq;
  67. typedef priority_queue<int, vi , greater<int> >minpq;
  68. typedef priority_queue<pi,vp,greater<pi> > mpq;
  69.  
  70. //Euclidean GCD
  71. //------------------------------------------
  72. //============================================================================
  73. int gcd(int A, int B) {
  74. if(B==0)
  75. return A;
  76. else
  77. return gcd(B, A % B);
  78. }
  79. //Fermat MMI
  80. //------------------------------------------
  81. //============================================================================
  82. //a^n % m
  83. ll mod_exponentiation(ll base, ll exponent, int modulus)
  84. {
  85. ll result = 1;
  86. while (exponent > 0)
  87. {
  88. if (exponent % 2 == 1)
  89. result = (result * base) % modulus;
  90. exponent = exponent >> 1;
  91. base = (base * base) % modulus;
  92. }
  93. return result;
  94. }
  95.  
  96. int fermat(ll n, ll m){
  97. return mod_exponentiation(n,m-2,m);
  98. }
  99.  
  100. struct node{
  101. int a;
  102. int cnt;
  103. };
  104. //custom comparator
  105. struct Compare{
  106. bool operator()(node &a, node &b){
  107. return a.cnt > b.cnt ;
  108. }
  109. };
  110.  
  111. //conversion
  112. //============================================================================
  113. //------------------------------------------
  114. inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
  115. template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
  116.  
  117. //math
  118. //-------------------------------------------
  119. //============================================================================
  120. template<class T> inline T sqr(T x) {return x*x;}
  121.  
  122.  
  123. //Main Starts
  124. //------------------------------------------
  125. //============================================================================
  126.  
  127. string a,b;
  128. vi pre,suf;
  129. bool flag = false;
  130. void prefix(){
  131. int i=0,j=0;
  132. while(i<sz(a) and j<sz(b)){
  133. if(b[j] == a[i]){
  134. pre.pb(i);
  135. i++;
  136. j++;
  137.  
  138. flag = true;
  139. }
  140. else
  141. i++;
  142. }
  143. cout<<"prefix"<<endl;
  144. tr(pre,it){
  145. cout<<*it<<" "<<a[*it]<<endl;
  146. }
  147. }
  148. void suffix(){
  149. int i=sz(a)-1,j=sz(b)-1;
  150. while(i>=0 and j>=0){
  151. if(b[j] == a[i]){
  152. suf.pb(i);
  153. i--;
  154. j--;
  155.  
  156. flag = true;
  157. }
  158. else
  159. i--;
  160. }
  161. }
  162.  
  163. int interruption(){
  164. if(pre[sz(pre)-1] <= suf[0])
  165. return 0;
  166. if(suf[sz(suf)-1] <= pre[0])
  167. return 0;
  168. return 1;
  169. }
  170. int main(){
  171. //ios_base::sync_with_stdio(false);cin.tie(NULL);
  172. cin>>a>>b;
  173. prefix();
  174. suffix();
  175. if(!flag){
  176. cout<<"-";
  177. return 0;
  178. }
  179. //trace2(a,b);
  180. sort(suf.begin() , suf.end());
  181. cout<<"suffix"<<endl;
  182. tr(suf,it){
  183. cout<<*it<<" "<<a[*it]<<endl;
  184. }
  185. int i1 =-1 , i2=sz(suf);
  186. int ans = 0;
  187. if(sz(pre) == 0){
  188. //cout<<"this 3 conditon is true\n";
  189. for(int i=0;i<sz(suf);i++)
  190. //if(suf[i]<sz(b))
  191. cout<<a[suf[i]];
  192. return 0;
  193. }
  194. if(sz(suf) == 0){
  195. //cout<<"this 2 conditon is true\n";
  196. for(int i=0;i<sz(pre);i++)
  197. //if(pre[i]<sz(b))
  198. cout<<a[pre[i]];
  199. return 0;
  200. }
  201. if(interruption()){
  202. cout<<"this interruption() conditon is true\n";
  203. if(sz(pre) > sz(suf)){
  204. for(int i=0;i<sz(pre);i++)
  205. //if(pre[i]<sz(b))
  206. cout<<a[pre[i]];
  207. }
  208. else{
  209. for(int i=0;i<sz(suf);i++)
  210. //if(suf[i]<sz(b))
  211. cout<<a[suf[i]];
  212. }
  213. return 0;
  214. }
  215. else{
  216. //cout<<"this 4 conditon is true\n";
  217. int len=-1;
  218. for(int i=0;i<sz(pre);i++){
  219. auto it = upper_bound(suf.begin(),suf.end(),pre[i]);
  220. if(it!=suf.end()){
  221. //trace1((sz(suf)-(it-suf.begin())));
  222. //trace1(i+1);
  223. len = i+1 + (sz(suf)-(it-suf.begin()));
  224. //trace1(len);
  225. if(len>ans){
  226. i1 = i;
  227. i2 = it-suf.begin();
  228. ans = len;
  229. }
  230. }
  231. else{
  232. len = i+1;
  233. if(len>ans){
  234. i1 = i;
  235. i2 = sz(suf);
  236. ans = len;
  237. }
  238. }
  239. }
  240. //trace2(i1,i2);
  241. //trace1(ans);
  242. if(ans > sz(b)){
  243. //cout<<"this 5 conditon is true\n";
  244. for(int i=0;i<sz(b);i++)
  245. cout<<b[i];
  246. return 0;
  247. }
  248. for(int i=0;i<=i1;i++){
  249. //if(pre[i]<sz(b))
  250. cout<<a[pre[i]];
  251. }
  252. for(int i=i2;i<sz(suf);i++){
  253. //sif(suf[i]<sz(b))
  254. cout<<a[suf[i]];
  255. }
  256. }
  257.  
  258.  
  259. }
  260.  
Success #stdin #stdout 0s 3480KB
stdin
abcdef
aceaf
stdout
prefix
0 a
2 c
4 e
suffix
0 a
5 f
this interruption() conditon is true
ace