4. (53rd Romanian Mathematical Olympiad (Final)) Find all real polynomials $f$ and $g$ such that for all $x \in \mathbf{R}$, we have $\left(x^{2}+x+\right.$ 1) $f\left(x^{2}-x+1\right)=\left(x^{2}-x+1\right) g\left(x^{2}+x+1\right)$.
4. Let $\omega$ be a non-real cube root of 1, satisfying $\omega^{2}+\omega+1=0$, then $g\left(\omega^{2}+\omega+1\right)=g(0)=0$.

Let $\alpha$ be a non-real cube root of -1, then $f\left(\alpha^{2}-\alpha+1\right)=f(0)=0$. Therefore, we can set $f(x)=x a(x), g(x)=x b(x)$. Thus, the original condition becomes
$$
a\left(x^{2}-x+1\right)=b\left(x^{2}+x+1\right) .
$$

Let $x=-y$, we get $a\left(y^{2}+y+1\right)=b\left(y^{2}-y+1\right)$, then
$$
b\left(x^{2}+x+1\right)=b\left[(x+1)^{2}-(x+1)+1\right]=a\left[(x+1)^{2}+(x+1)+1\right] \text {. }
$$

Hence $a\left(x^{2}-x+1\right)=a\left[(x+1)^{2}+(x+1)+1\right]$.
We will prove that there are infinitely many $n$ such that
$$
a\left(n^{2}+3 n+3\right)=a(1) \text {. }
$$

By $x=1$ we get $a(1)=a(7)$.
Assume $a\left[(n-1)^{2}+3(n-1)+3\right]=a(1)(n \geqslant 2)$, then
$$
\begin{aligned}
& a\left[(n+1)^{2}+3(n+1)+3\right]=a\left[(n+2)^{2}+(n+2)+1\right] \\
= & a\left[(n+1)^{2}-(n+1)+1\right]=a\left[(n-1)^{2}+3(n-1)+3\right]=a(1) .
\end{aligned}
$$

Since the polynomial $a(x)-a(1)$ has infinitely many roots, $a(x)-a(1)$ is the zero polynomial, i.e., $a(x)$ is a constant. Therefore, $f(x)=k x$. Similarly, we get $g(x)=k x$.