http://acm.hdu.edu.cn/showproblem.php?pid=4731
就做了两道...也就这题还能发博客了...虽然也是水题
先暴力DFS打表找规律...发现4个一组循环节...尾部特殊判断....然后构造一下...
#include <cstdio> #include <string> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #include <vector> using namespace std; int ss; int f(const string& strs) { string s("##"); for(string::const_iterator it = strs.begin(); it != strs.end(); ++it) { s.push_back(*it); s.push_back('#'); } int len = s.length(), id = 0, mx = 0; vector<int> p(len, 0); for(int i = 1; i < len; ++i) { p[i] = mx > i ? min(mx - i, p[2*id-i]) : 1; while(s[i+p[i]] == s[i-p[i]]) { ++p[i]; } if(i + p[i] > mx) { mx = i + p[i]; id = i; } } int start = 0, sublen = 0; for(int i = 1; i < len; ++i) { if(p[i] > sublen) { sublen = p[i]; start = i; } } return sublen - 1; } int main(){ //pre(); int n,m; int T; cin>>T; for(int t = 1 ; t <= T ; t++){ scanf("%d%d",&m,&n); printf("Case #%d: ",t); if(m == 1){ for(int i = 0 ; i < n ; i++) printf("a"); } else if(m >= 3){ for(int i = 0 ; i < n ; i++) { if(i%3 == 0) putchar('a'); if(i%3 == 1) putchar('b'); if(i%3 == 2) putchar('c'); } }else if(m == 2){ if(n == 1) printf("a"); if(n == 2) printf("ab"); if(n == 3) printf("aab"); if(n == 4) printf("aabb"); if(n == 5) printf("aaaba"); if(n == 6) printf("aaabab"); if(n == 7) printf("aaababb"); if(n == 8) printf("aaababbb"); if(n >= 9){ string a = "aaaa"; for(int i = 4; i < n ;){ if(i+4<n) { a+="babb"; i+=4; } if(i+4>=n) { if(i+1==n) a+="a"; if(i+2==n) a+="aa"; if(i+3==n) a+="aaa"; if(i+4==n) a+="aaaa"; break; } if(i+4<n) { a+="aaba"; i+=4; } if(i+4>=n) { if(i+1==n) a+="b"; if(i+2==n) a+="bb"; if(i+3==n) a+="bba"; if(i+4==n) a+="bbaa"; break; } if(i+4<n) { a+="bbaa"; i+=4; } if(i+4>=n) { if(i+1==n) a+="a"; if(i+2==n) a+="aa"; if(i+3==n) a+="bab"; if(i+4==n) a+="babb"; break; } } cout<<a; } } putchar(' '); } return 0; }