#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string.h>
#include<algorithm>
#include<memory.h>
#include<stdio.h>
#include<bitset>
#include<vector>
#include<set>
#include<time.h>
using namespace std;
typedef long long ll;
const int N = 1002;
char a[N], b[N];
int dp[N][N];
int n, m;
int nxt[26][N];
int oo = 1e6;
int calc(int i, int j){
if (i == n)
return oo;
if (j == m)
return 1;
if (dp[i][j] != -1)
return dp[i][j];
int ret = calc(i + 1, j);
int idx = nxt[a[i] - 'a'][j];
if (idx == -1)
ret = min(ret, calc(i + 1, m));
else
ret = min(ret, calc(i + 1, nxt[a[i] - 'a'][j] + 1) + 1);
return dp[i][j] = ret;
}
void print(int i, int j){
if (j == m){
printf("%c\n", a[i]);
return;
}
int best = calc(i, j);
int idx = nxt[a[i] - 'a'][j];
if (idx == -1 && best == 1){
printf("%c\n", a[i]);
return;
}
else if (best == calc(i + 1, nxt[a[i] - 'a'][j] + 1) + 1){
printf("%c", a[i]);
print(i + 1, nxt[a[i] - 'a'][j] + 1);
}
else print(i + 1, j);
}
int main(){
memset(dp, -1, sizeof(dp));
scanf("%s%s", &a, &b);
n = strlen(a);
m = strlen(b);
for (int i = 0; i < 26; ++i)
nxt[i][m - 1] = -1;
nxt[b[m - 1] - 'a'][m - 1] = m - 1;
for (int i = m - 2; i >= 0; --i){
for (int j = 0; j < 26; ++j)
nxt[j][i] = nxt[j][i + 1];
nxt[b[i] - 'a'][i] = i;
}
printf("%d\n", calc(0, 0));
print(0, 0);
}
I2RlZmluZSBfQ1JUX1NFQ1VSRV9OT19XQVJOSU5HUwojaW5jbHVkZTxpb3N0cmVhbT4KI2luY2x1ZGU8c3RyaW5nLmg+CiNpbmNsdWRlPGFsZ29yaXRobT4KI2luY2x1ZGU8bWVtb3J5Lmg+CiNpbmNsdWRlPHN0ZGlvLmg+CiNpbmNsdWRlPGJpdHNldD4KI2luY2x1ZGU8dmVjdG9yPgojaW5jbHVkZTxzZXQ+CiNpbmNsdWRlPHRpbWUuaD4Kd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