Example 5 Find a polynomial $p(x)$ such that $\left(x^{2}+1\right) \mid p(x), \left(x^{3}+x^{2}+1\right) \mid p(x)+1$.

Translate the above text into English, please retain the original text's line breaks and format, and output the translation result directly.
Analysis Given that $p(x)=\left(x^{2}+1\right) g(x), p(x)+1=\left(x^{3}+x^{2}+1\right) h(x)$, we have $\left(x^{3}+x^{2}+1\right) h(x)-\left(x^{2}+1\right) g(x)=1$. To find $g(x)$ and $h(x)$, we can use the Euclidean algorithm.

Solution From the given conditions, for the required $p(x)$, there exist polynomials $g(x)$ and $h(x)$ such that $p(x)=\left(x^{2}+1\right) g(x)$ and $p(x)+1=\left(x^{3}+x^{2}+1\right) h(x)$. Therefore, $\left(x^{3}+x^{2}+1\right) h(x)-\left(x^{2}+1\right) g(x)=1$.
We will use the Euclidean algorithm to find $g(x)$ and $h(x)$. First, we have
$$
\begin{array}{l}
x^{3}+x^{2}+1=(x+1)\left(x^{2}+1\right)+(-x), \\
x^{2}+1=(-x)(-x)+1 .
\end{array}
$$

By reversing the steps, we get
$$
\begin{aligned}
1 & =\left(x^{2}+1\right)+x(-x) \\
& =\left(x^{2}+1\right)+x\left[\left(x^{3}+x^{2}+1\right)-(x+1)\left(x^{2}+1\right)\right. \\
& \left.=\left(x^{2}+1\right)[1-x(x+1)]+x\left(x^{3}+x^{2}+1\right)\right] \\
& =\left(x^{3}+x^{2}+1\right) x-\left(x^{2}+1\right)(x+x-1) .
\end{aligned}
$$

Thus, we can take $h(x)=x$ and $g(x)=x^{2}+x-1$, so one solution for $p(x)$ is $\left(x^{2}+1\right)\left(x^{2}+x-1\right)$ (the solution is not unique).