https://www.acwing.com/problem/content/description/4720/
对于环形构造来说肯定是要留一个部分进行去重的,然后用小于整个字符串的长度进行构造这是最简单的,然而刚开始用大于7的字符串构造是很复杂而且不好考虑环形的重复部分,而且考虑构造字符串的数目也是跟题目中的每相邻四个不重复是关联的
而我自己的构造法相当是一种构造的字符串数目大于7的暴力方法,需要考虑判重的条件很多,没有利用好每四个相邻的条件和字符串构造相结合
y总构造法
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
string cs = "ROYGBIV";
string res;
int n;
cin >> n;
for(int i = 4; i <= 6; i ++) res += cs[i];
for(int i = 0; i < n - 3; i ++) res += cs[i % 4];
cout << res;
return 0;
}
me
#include<iostream>
using namespace std;
string a = "ROYGBIV";
string b = "GBIVYGB";
string m = "IVB";
int main()
{
int n;
cin >> n;
int k = n / 7;
int q = k;
int s = n % 7;
if(k == 1 && s == 0) cout << "ROYGBIV";
else
if(k % 2 == 0)
{
k = k/2;
for(int i = 1; i <= k; i ++)
cout << "ROYGBIVGBIVYGB";
if(s != 0)
{
if(s >= 4)
{
for(int i = 0; i < s; i ++)
cout << a[i];
}
else {
for(int i = 0; i < s; i ++)
cout << m[i];
}
}
}
else if(q % 2 != 0 && s != 0)
{
if(q == 1) cout << "ROYGBIV";
else
{
for(int i = 1; i <= q /2; i ++)
cout << "ROYGBIVGBIVYGB";;
if(q % 2 != 0) cout << "ROYGBIV";
}
if(s != 5)
{
for(int i = 0; i < s; i ++)
cout << b[i];}
else
{
cout << "GBIVG";
}
}
}