Uva129_構造困難的字符串


//通過回溯的方法構造,通過週期的性質進行檢查
#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

發佈了59 篇原創文章 · 獲贊 20 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章