M=1:aaaaaaaa……
M=2:DFS+manacher, 暴出N=1~25的最优解,找规律。N<=8的时候直接输出,N>8时,头两个字母一定是aa,剩下的以aababb循环,最后剩余<5全部补a,等于5补aabab。
M=3:abcabcabcabc……
#include <cstdio> #include <cstring> using namespace std; const char str[] = "aababb"; int main() { int T; int cas = 0; scanf( "%d", &T ); while ( T-- ) { int M, N; scanf( "%d%d", &M, &N ); printf("Case #%d: ", ++cas ); if ( M == 1 ) { for ( int i = 0; i < N; ++i ) putchar('a'); puts(""); } else if ( M == 2 ) { int cnt; switch( N ) { case 1: puts("a"); break; case 2: puts("ab"); break; case 3: puts("aab"); break; case 4: puts("aabb"); break; case 5: puts("aaaba"); break; case 6: puts("aaabab"); break; case 7: puts("aaababb"); break; case 8: puts("aaababbb"); break; default: N -= 2; printf("aa"); cnt = N / 6; for ( int i = 0; i < cnt; ++i ) printf( "%s", str ); N -= cnt*6; if ( N < 5 ) { for ( int i = 0; i < N; ++i ) putchar('a'); puts(""); } else puts("aabab"); } } else { int j = 0; for ( int i = 0; i < N; ++i ) { putchar( 'a' + j ); ++j; if ( j > 2 ) j = 0; } puts(""); } } return 0; }