Uva129_构造困难的字符串

来源:互联网 发布:识别蘑菇的软件 编辑:程序博客网 时间:2024/05/14 14:42
//通过回溯的方法构造,通过周期的性质进行检查#include<iostream>using namespace std;int n, l;const int maxloc = 100;int S[maxloc];int cnt;bool check(int loc){    for (int r = 1; loc - 2 * r + 1 >= 0; r++)    {        bool f = true;        for (int i = 0; i<r; i++)        {            if (S[loc - i] != S[loc - i - r])            {                f = false;                break;            }        }        if (f)            return false;    }    return true;}bool fun(int loc){    if (cnt == l)    {        for (int i = 0; i<loc; i++)        {            if (i) cout << " ";            cout << (char)('A' + S[i]);        }        cout << endl;        return 1;    }    else    {        for (int i = 0; i<n; i++)        {            S[loc] = i;            if (check(loc))            {                cnt++;                if (fun(loc + 1))                    return 1;            }        }    }    return 0;}int main(){    //用数字代替字符    cin >> n >> l;    cnt = 0;    if (!fun(0))    {        cout << "No ans" << endl;    }    return 0;}

剩下992

0 0
原创粉丝点击