hdu

http://acm.hdu.edu.cn/showproblem.php?pid=1627

给定 n 和 L 找出第n个范围在0-L之内的字符串,字符串要求没有相邻的子串是相同的。

按照格式输出。

这个题的关键在于判断字符串是否是符合要求的字符串.

枚举字符串所有子串可能的长度,然后判断即可。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val)    memset(arr, val, sizeof(arr))

#define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0)

#define L(x)    (x) << 1
#define R(x)    (x) << 1 | 1
#define MID(l, r)   (l + r) >> 1
#define Min(x, y)   (x) < (y) ? (x) : (y)
#define Max(x, y)   (x) < (y) ? (y) : (x)
#define E(x)        (1 << (x))
#define iabs(x)     (x) < 0 ? -(x) : (x)
#define OUT(x)  printf("%I64d
", x)
#define lowbit(x)   (x)&(-x)
#define Read()  freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 1000000000
#define N 2510
#define mod 1000000000
using namespace std;
int n,m;
int a[100];

void dfs(int cur)
{
    for(int i=0;i<m;i++)
    {
        a[cur]=i;
        bool is=0;
        for(int j=1;2*j<=cur+1;j++)  //枚举 所有 的可能长度,
        {
            bool flag=0;
            for(int k=0;k<j;k++) //判断是否符合要求
                if(a[cur-k]!=a[cur-j-k]) {flag=1;break;}
            if(!flag)
            {
                is=1;
                break;
            }
        }
        if(is) continue;
        if(--n==0)
        {
            for(int j=0;j<=cur;j++)
            {
                if(j&&j%64==0) printf("
");
                else if(j&&j%4==0) printf(" ");
                printf("%c",a[j]+'A');
            }
            printf("
");
            printf("%d
",cur+1);
        }
        dfs(cur+1);
        if(n==0) return;
    }
}
int main()
{
   //freopen("a.txt","r",stdin);
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0) break;
        dfs(0);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/nowandforever/p/4547547.html