/*
ID: wangzha4
LANG: C++
TASK: lgame
*/
/*
Test 1: TEST OK [0.032 secs, 3352 KB]
Test 2: TEST OK [0.043 secs, 3348 KB]
Test 3: TEST OK [0.065 secs, 3352 KB]
Test 4: TEST OK [0.032 secs, 3352 KB]
Test 5: TEST OK [0.043 secs, 3348 KB]
Test 6: TEST OK [0.043 secs, 3348 KB]
Test 7: TEST OK [0.043 secs, 3352 KB]
Test 8: TEST OK [0.043 secs, 3352 KB]
Test 9: TEST OK [0.043 secs, 3352 KB]
Test 10: TEST OK [0.043 secs, 3352 KB]
Test 11: TEST OK [0.032 secs, 3348 KB]
Test 12: TEST OK [0.032 secs, 3348 KB]
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define llong unsigned long long
#define unint unsigned int
#define printline printf( "\n" )
const int INF = 1000000 ;
const int size = 155 ;
const int table[27] = { 2,5,4,4,1,6,5,5,1,7,6,3,5,2,3,5,7,2,1,2,4,6,6,7,5,7 } ;
char instr[27] ;
int len, val ;
int flag[150] = {0} ;
struct DICT
{
char str[8] ;
int val ;
int len ;
};
struct DICT dict[40005] ;
int ct_dict = 0 ;
char out[1000][10] ;
int ct_out = 0 ;
int bestval = -1 ;
int cmp( const void *a, const void *b )
{
struct DICT *c = (struct DICT *)a ;
struct DICT *d = (struct DICT *)b ;
if( d->len != c->len )
return d->len - c->len ;
else
return c->val - d->val ;
}
void init()
{
memset( flag, 0, sizeof(flag) ) ;
len = strlen( instr ) ;
for( int i=0; i<len; i++ ) flag[instr[i]] ++ ;
freopen( "lgame.dict", "r", stdin ) ; char indict[10] ;
while( scanf( "%s", indict ) != EOF )
{
if( '.' == indict[0] ) break ;
int tflag[150] = {0} ;
memset( tflag, 0, sizeof(tflag) ) ;
int curlen = strlen( indict ) ; bool inword = true ;
for( int i=0; i<curlen; i++ ) {
if( !flag[indict[i]] ) { inword = false ; break ; }
tflag[indict[i]] ++ ;
if( tflag[indict[i]] > flag[indict[i]] ) { inword = false ; break ; }
}
if( inword )
{
dict[ct_dict].len = curlen ;
strcpy( dict[ct_dict].str, indict ) ;
dict[ct_dict].val = 0 ;
for( int i=0; i<curlen; i++ )
dict[ct_dict].val += table[indict[i]-'a'] ;
ct_dict ++ ;
}
}
qsort( dict, ct_dict, sizeof(dict[0]), cmp ) ;
}
void process()
{
bestval = -1 ; int tflag[150] ; int ttflag[150] ;
for( int i=0; i<ct_dict; i++ )
{
if( dict[i].val > bestval )
{
bestval = dict[i].val ;
ct_out = 0 ;
strcpy( out[ct_out++], dict[i].str ) ;
}
else if( dict[i].val == bestval )
{
strcpy( out[ct_out++], dict[i].str ) ;
}
//else //注意这个地方不要用else
{
for( int j=ct_dict-1; j>i; j-- )
{
if( dict[i].len + dict[j].len > len ) break ;
if( dict[i].val + dict[j].val < bestval ) continue ;
memset( tflag, 0, sizeof(tflag) ) ;
memset( ttflag, 0, sizeof(ttflag) ) ;
for( int k=0; k<dict[i].len; k++ ) ttflag[dict[i].str[k]]++ ;
for( int k=0; k<dict[j].len; k++ ) tflag[dict[j].str[k]] ++ ;
int curval = 0 ; bool canUnion = true ;
for( int k=0; k<dict[j].len; k++ )
{
if( tflag[dict[j].str[k]] + ttflag[dict[j].str[k]] > flag[dict[j].str[k]] )
{ canUnion = false ; break ; }
}
if( canUnion )
{
curval = dict[i].val + dict[j].val ;
if( curval > bestval )
{
bestval = curval ;
ct_out = 0 ;
if( strcmp( dict[i].str, dict[j].str ) < 0 ) {
strcpy( out[ct_out], dict[i].str ) ;
out[ct_out][dict[i].len] = ' ' ;
out[ct_out][dict[i].len+1] = '\0' ;
strcat( out[ct_out], dict[j].str ) ;
ct_out++ ;
}
else {
strcpy( out[ct_out], dict[j].str ) ;
out[ct_out][dict[j].len] = ' ' ;
out[ct_out][dict[j].len+1] = '\0' ;
strcat( out[ct_out], dict[i].str ) ;
ct_out++ ;
}
}
else if( curval == bestval )
{
if( strcmp( dict[i].str, dict[j].str ) < 0 ) {
strcpy( out[ct_out], dict[i].str ) ;
out[ct_out][dict[i].len] = ' ' ;
out[ct_out][dict[i].len+1] = '\0' ;
strcat( out[ct_out], dict[j].str ) ;
ct_out++ ;
}
else {
strcpy( out[ct_out], dict[j].str ) ;
out[ct_out][dict[j].len] = ' ' ;
out[ct_out][dict[j].len+1] = '\0' ;
strcat( out[ct_out], dict[i].str ) ;
ct_out++ ;
}
}
}
}
}
}
}
int outcmp( const void *a, const void *b )
{
char *c = (char *)a ; char *d = (char *)b ;
return strcmp( c, d ) ;
}
void output()
{
qsort( out, ct_out, sizeof(out[0]), outcmp ) ;
printf( "%d\n",bestval ) ;
for( int i=0; i<ct_out; i++ ) printf( "%s\n", out[i] ) ;
}
int main()
{
freopen( "lgame.in", "r", stdin ) ;
freopen( "lgame.out","w",stdout ) ;
while( scanf( "%s", instr ) != EOF )
{
init() ;
process() ;
output() ;
}
return 0 ;
}