题本身挺水,就是有点坑人,字典大概10W个,用Trie树存会MLE,用map存就好,字符串有26种变化,枚举一遍,输出单词数最小的,n^2 的dp解决,还有一个坑,The answer will be considered correct if no adjacent words are single character and the average number of characters in the words is greater than 2. 加个特判就行了
大概就是这样
Memory: 9988K Time: 1188MS
Language: C++ Result: Accepted
# include <iostream>
# include <set>
# include <map>
# include <vector>
# include <list>
# include <queue>
# include <stack>
# include <cstring>
# include <string>
# include <cstdlib>
# include <cmath>
# include <algorithm>
using namespace std ;
const int MAX = 1e9 ;
string t ;
int cnt , len ;
string ss [ 100000 ] ;
map < string , int > mmap ;
vector < int > vec ;
int stmin , k , ansk ;
void DP ( ){
int llist [ 300 ] = { 0 } ;
int num [ 300 ] = { 0 } ;
int dp [ 300 ] = { 0 } ;
memset ( dp , -1 , sizeof ( dp ) ) ;
dp [ 0 ] = 0 ;
for ( int i = 1 ; i <= len ; i ++ ) {
int minn = MAX , pos = -1 ;
map < string , int > :: iterator tmp ;
for ( int j = 0 ; j < i ; j ++ ) {
if ( j == i - 1 ) {
if ( llist [ i - 1 ] == i - 2 ) break ;
}
if ( dp [ j ] != -1 && dp [ j ] + 1 < minn ) {
string s ;
for ( int h = j ; h < i ; h ++ )
s += t [ h ] ;
map < string , int > :: iterator it = mmap . find ( s ) ;
if ( it != mmap . end ( ) ) {
minn = dp [ j ] + 1 ;
pos = j ;
tmp = it ;
}
}
}
if ( minn != MAX ) {
dp [ i ] = minn ;
num [ i ] = tmp -> second ;
llist [ i ] = pos ;
}
}
if ( dp [ len ] != -1 && dp [ len ] < stmin && len > dp [ len ] * 2 ) {
stmin = dp [ len ] ;
ansk = k ;
vec . clear ( ) ;
for ( int i = len ; i ; i = llist [ i ] ) {
vec . push_back ( num [ i ] ) ;
}
}
}
int main ( )
{
cin . sync_with_stdio ( false ) ;
while ( getline ( cin , t ) , t [ 0 ] ) {
mmap [ t ] = cnt ;
ss [ cnt ++ ] = t ;
}
while ( getline ( cin , t ) , t [ 0 ] != '0' ) {
stmin = MAX ;
len = t . size ( ) ;
for ( k = 0 ; k < 26 ; k ++ ) {
DP ( ) ;
for ( int i = 0 ; i < len ; i ++ ) {
t [ i ] = ( t [ i ] -'a' + 25 ) % 26 + 'a' ;
}
}
if ( stmin == MAX ) cout << "NO SOLUTIONS" << endl ;
else {
cout << "k=" << ansk << ':' ;
for ( int i = vec . size ( ) - 1 ; i >= 0 ; i -- ) {
cout << ' ' << ss [ vec [ i ] ] ;
}
cout << endl ;
}
}
}