暑假集训单切赛第一场 UVA 1737 Mnemonics and Palindromes 3

题意:求由a,b,c三个字母组成的长度为n的字符串,其任意连续的至少长度大于等于2的子字符串都不是回文,问这个字符串有多少种?并字典序输出   

   如果所有种类的字符串总长度大于100000个字符,就输出TOO LONG.

思路:先动手写写    如前两个ab,则第三个不能为a,b,只能为c。    接下来,不能为b,c,只能为a。    于是找出规律,某一位上的字母不能与前两位的相同,这样由开头两个字母就唯一确定一串字符串      

   于是开头两个字母,只有6种,所以不管n(n>=2)取多少,满足要求的都只有6种。      

   由于样例可能为n=1,所以当n=1时,只要输出a b c即可。

 

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <queue>

using namespace std;
char s[6][3]= {"ab","ac","ba","bc","ca","cb"};
char ch[3]= {'a','b','c'};
int n,length;
int main() {
    scanf("%d",&n);
    if(n*6>100000)
        printf("%TOO LONG\n");
    else if(n==1) {
        for(int i=0; i<3; i++)
            printf("%c\n",ch[i]);
    } else {
        for(int i=0; i<=5; i++) {
            length=2;
            printf("%s",s[i]);

            while(length<n) {
                for(int j=0; j<3; j++) {
                    if(s[i][0]!=ch[j] && s[i][1]!=ch[j]) {
                        printf("%c",ch[j]);
                        s[i][0]=s[i][1];
                        s[i][1]=ch[j];
                        length++;
                        break;
                    }
                }
            }
            printf("\n");

        }
    }
    return 0;
}

 

 

 

posted @ 2013-08-27 17:59  辰曦~文若  阅读(226)  评论(0编辑  收藏  举报