手上没有编译器,用 https:// 编译了一下,没法保证正确,仅供参考
程序代码:
#include <stdio.h>
#include <string.h>
void foo( char* s, unsigned n, unsigned m )
{
m = m<=n?m:n;
if( m == 0 )
return;
char minchar = s[0];
for( unsigned i=1; i!=m+1; ++i )
minchar = minchar<=s[i]?minchar:s[i];
char s_min[106];
strcpy( s_min, s );
for( unsigned i=0; i!=m+1; ++i )
{
if( minchar != s[i] )
continue;
char s2[106];
strcpy( s2, s+i );
foo( s2+1, n-i-1, m-i );
if( strcmp(s2,s_min) < 0 )
strcpy( s_min, s2 );
}
strcpy( s, s_min );
}
int main( void )
{
unsigned t;
scanf( "%u", &t );
while( t-- )
{
unsigned n, m; char s[106];
scanf( "%u%u%s", &n, &m, s );
foo( s, n, m );
puts( s );
}
}