求解,40行一直被說多一個空格(WA: ABAC....ABAE )
自己Codeblocks跑好像是對的
#include <iostream>
int main()
{
int n, L;
while (std::cin >> n >> L)
{
if (!n)
break;
char alt[80];//contains one alt sol to fall back to
char arr[80];//contains sol
char temp_sol = 0;//sol for one iteration
for (int i = 0; i < 80; ++i)//init
{
arr[i] = 0;
alt[i] = 0;
}
int current_pos = 0;
for (int i = 0; i < n; ++i)//do n iterations
{
bool it_done = 0;
temp_sol = 0;//reset for each iteration
int starting_letter = 0;
while (!it_done)
{
for (int j = starting_letter; j < L; ++j)//check valid letter
{
bool match = 0;//if no matching seq, letter is valid
arr[current_pos] = 'A' + j;//test fill a letter
//int min_check_pos = (current_pos + 1) / 2 - 1;
//if (min_check_pos < 0)
// min_check_pos = 0;
int min_check_pos = current_pos / 2;
for (int check_pos = current_pos - 1; check_pos >= min_check_pos; --check_pos)//back check all seq.
{
match = 1;
for (int check_pos_copy = check_pos, current_pos_copy = current_pos;
current_pos_copy > check_pos;)//check that seq from check_pos
{
if (arr[check_pos_copy] != arr[current_pos_copy])//not a matching seq
{
match = 0;
break;//check next seq
}
--check_pos_copy;
--current_pos_copy;
}
if (match)//match found
{
break;//letter invalid
}
}
if (!match)//valid
{
if(!temp_sol)
temp_sol = arr[current_pos];
else if (!alt[current_pos])
{
alt[current_pos] = arr[current_pos];
arr[current_pos] = temp_sol;
break;//two sol found, no need to check further
}
}
}
if (temp_sol)//there is valid letter(one or more)
{
arr[current_pos] = temp_sol;//retrieve data from temp_sol
++current_pos;
it_done = 1;
}
else//no valid letter, have to fall back
{
int search_pos = current_pos;
while (!alt[search_pos])
--search_pos;
for (; current_pos > search_pos; --current_pos)
arr[current_pos] = 0;
temp_sol = alt[current_pos];
alt[current_pos] = 0;
starting_letter = temp_sol - 'A' + 1;
}
}
}
int i = 0;
for (; arr[i] > 0; ++i)
{
if (i)
{
if ((i % 64) == 0)
std::cout << "\n";
else if ((i % 4) == 0)
std::cout << " ";
}
std::cout << arr[i];
}
std::cout << "\n" << i << "\n";
}
return 0;
}