#P2377. 三角形图

三角形图

Description

UNowen has nn plane vectors {Ln}\{\bm L_n\}, where:

  • L1=L2==Ln=1|\bm L_1|=|\bm L_2|=\ldots=|\bm L_n|=1;
  • L1+L2++Ln=0\bm L_1+\bm L_2+\ldots+\bm L_n=\bm 0;
  • For any positive integers 1i,jn1 \le i,j \le n, there always exists an integer kk such that Li,Lj=kπ3\langle\bm L_i,\bm L_j\rangle=\dfrac{k\pi}{3}; (kπ3=k×60\dfrac{k\pi}{3}=k \times 60^\circ)
  • Except for the entire vector sequence (same below), there is no contiguous subsequence in the vector sequence whose sum is 0\bm 0.

Here X,Y\langle\bm X,\bm Y\rangle denotes “the angle by which X\bm X is rotated to point in the same direction as Y\bm Y,” where counterclockwise is positive and clockwise is negative (for example, let X=(22,22)\bm X=(\dfrac{\sqrt{2}}{2},\dfrac{\sqrt{2}}{2}), Y=(1,0)\bm Y=(1,0), then X,Y=π4\langle\bm X,\bm Y\rangle=-\dfrac{\pi}{4}).

In other words, all nn vectors have length 11, and concatenating them head to tail forms a closed polygon. It is guaranteed that each interior angle of this polygon is an integer multiple of π3\dfrac{\pi}{3}, its interior contains no other vectors, and it is not hollow.

For convenience, we use a string SS of length nn over the alphabet {a,b,c,d,e,#}\{\tt a,\tt b,\tt c,\tt d,\tt e,\#\} to describe {Ln}\{\bm L_n\}.

Specifically, for the ii-th character in SS (in particular, L0\bm L_0 is equivalent to Ln\bm L_n, and Ln+1\bm L_{n+1} is equivalent to L1\bm L_1, similarly below):

字符 含义
a\tt a Li,Li+1=2π3\langle\bm L_i,\bm L_{i+1}\rangle=\dfrac{2\pi}{3}
b\tt b Li,Li+1=π3\langle\bm L_i,\bm L_{i+1}\rangle=\dfrac{\pi}{3}
c\tt c Li,Li+1=0\langle\bm L_i,\bm L_{i+1}\rangle=0
d\tt d Li,Li+1=π3\langle\bm L_i,\bm L_{i+1}\rangle=-\dfrac{\pi}{3}
e\tt e Li,Li+1=2π3\langle\bm L_i,\bm L_{i+1}\rangle=-\dfrac{2\pi}{3}
#\tt \# Li,Li+1=π\vert\langle\bm L_i,\bm L_{i+1}\rangle\vert=\pi

In addition, UNowen defines a “standard representation” of a vector sequence {L1,L2,,Ln}\{\bm L_1,\bm L_2,\ldots,\bm L_n\} as follows:

  • For l=1,2,,nl=1,2,\ldots,n, compute the string corresponding to the vector sequence $\{\bm L_l,\bm L_{l+1},\ldots,\bm L_n,\bm L_1,\bm L_2,\ldots,\bm L_{l-1}\}$.
  • Among these nn strings, take the lexicographically smallest string as the “standard representation” of the vector sequence {L1,L2,,Ln}\{\bm L_1,\bm L_2,\ldots,\bm L_n\}.

For example, the “standard representation” of the vector sequence in the figure above is S=abcedcdcdddeS=\texttt{abcedcdcddde}, and not S=dcdcdddeabceS=\texttt{dcdcdddeabce}.

Now, UNowen gives you the string SS corresponding to the vector sequence {L1,L2,,Ln}\{\bm L_1,\bm L_2,\ldots,\bm L_n\} (which is not necessarily its “standard representation”). He hopes you can perform one modification to this vector sequence as follows:

  • Choose a positive integer 1kn1 \le k \le n.
  • Construct two plane vectors X,Y\bm X,\bm Y such that:
    • X=1|\bm X|=1;
    • Y=1|\bm Y|=1;
    • Lk,X=π3\langle\bm L_k,\bm X\rangle=\dfrac{\pi}{3};
    • Lk,Y=π3\langle\bm L_k,\bm Y\rangle=-\dfrac{\pi}{3}.
  • Replace Lk\bm L_k with two items X,Y\bm X,\bm Y (with X\bm X before Y\bm Y).
  • If Lk1+X=0\bm L_{k-1}+\bm X=\bm 0, delete Lk1\bm L_{k-1} and X\bm X.
  • If Lk+1+Y=0\bm L_{k+1}+\bm Y=\bm 0, delete Lk+1\bm L_{k+1} and Y\bm Y.
  • If after the modification there exists a contiguous subsequence whose sum is 0\bm 0 (other than the entire vector sequence), then UNowen considers this modification invalid. For example, in the figure below, if you choose k=3k=3, then the modification is invalid.
  • UNowen defines the “modification result” as the “standard representation” of the modified vector sequence.
  • For any two modification schemes, if the polygons obtained by concatenating the modified vector sequences head to tail are congruent, then UNowen considers the two modification schemes equivalent. For example, in the figure below, the two modification schemes k=1k=1 and k=2k=2 are equivalent.
  • That is, the modification operation constructs an outward equilateral triangle on some vector of the closed polygon, such that the modified shape is still a closed polygon satisfying the above requirements (each interior angle is an integer multiple of π3\dfrac{\pi}{3}, the interior contains no other vectors, and it is not hollow). For any two modification schemes, if the polygons obtained from the modified sequences are congruent, then they are equivalent.

Find the number of pairwise non-equivalent valid modification schemes, and output the “modification result” of each scheme in lexicographic order.

Input Format

This problem contains multiple test cases.

The first line contains a positive integer TT, the number of test cases.

The next TT lines each contain a non-empty string SS over the alphabet {a,b,c,d,e}\{\tt a,\tt b,\tt c,\tt d,\tt e\}. It is guaranteed that #\tt \# will not appear.

Output Format

For each test case, output three lines:

  • The first line contains a positive integer KK, the number of pairwise non-equivalent valid modification schemes.
  • The second line contains KK non-empty strings over the alphabet {a,b,c,d,e}\{\tt a,\tt b,\tt c,\tt d,\tt e\} (separated by spaces), representing each possible “modification result.” You must output them in lexicographic order. It can be proven that #\tt \# will not appear.
  • The third line is an empty line. Please pay special attention to this, as it does not follow the usual data format of traditional problems.
2
eee
cedde

1
dede

3
beddde cdecde cecece

2
adeccecced
dddddd

5
acedccecced addebcecced adebebecced adecbedcced cceccecce

1
cddddce

7
eee
cedde
adeccecced
dddddd
ccdeccde
baedddcdde
abcedcdcddde
1
dede

3
beddde cdecde cecece

5
acedccecced addebcecced adebebecced adecbedcced cceccecce

1
cddddce

4
bcdeccdde bcedccece bdeccdebe cccedccde

8
abdecdcddde abececcddde abedcebddde abeddbecdde abeddccecde abeddcdcece abeddcddced addddcdde

10
abbeddcdcddde abcdeccdcddde abcecebdcddde abcedbeccddde abcedccebddde abcedcdbecdde abcedcdccecde abcedcdcdcece abcedcdcddced acedcdcdddd

Hint

Let S|S| be the length of the string SS.

  • For 30%30\% of the testdata, T5T \le 5, S9|S| \le 9.
  • For 50%50\% of the testdata, it is guaranteed that all modification schemes are valid.
  • For 100%100\% of the testdata, 1T1001 \le T \le 100, 3S753 \le |S| \le 75.

Translated by ChatGPT 5