字符序列

来源:互联网 发布:网络拓扑图生成 编辑:程序博客网 时间:2024/06/06 02:54

Problem Description

从三个元素的集合[A,B,C]中选取元素生成一个N个字符组成的序列,使得没有两个相邻字的子序列(子序列长度=2)相同。例:N=5时ABCBA是合格的,而序列ABCBC与ABABC是不合格的,因为其中子序列BC,AB是相同的。
对于由键盘输入的N(1<=N<=12),求出满足条件的N个字符的所有序列总数。

Input

输入有多组数据,每组数据只有一行为一个整数N。

Output

对于每组数据满足条件的N个字符的所有序列总数

Sample Input

4

Sample Output

72
//解题报告:连续四个字符,左边的字串(长度为2)和右边的字串(长度为2)不能相等。
//标程:
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int temp[20], cnt, ans[20];void dfs(int x, int step){if(x == step){         cnt ++; return ;}for(int i = 1; i <= 3; i ++){        if(temp[x-3]*10+temp[x-2] != temp[x-1]*10+i){temp[x] = i;dfs(x+1,step);}}}int main(){//freopen("a.txt","r",stdin);    int n, i, j, k, x;memset(ans,0,sizeof(ans));ans[1] = 3, ans[2] = 9, ans[3] = 27;for(i = 4; i <= 12; i ++){cnt = 0;memset(temp,0,sizeof(temp));for(j = 1; j <= 3; j ++)for(k = 1; k <= 3; k ++)for(x = 1; x <= 3; x ++){temp[0] = j, temp[1] = k, temp[2] = x; dfs(3,i);} ans[i] = cnt;}while(cin >> n) cout << ans[n] << endl;return 0;}
0 0
原创粉丝点击