It takes $A$ algebra books (all the same thickness) and $H$ geometry books (all the same thickness, 
which is greater than that of an algebra book) to completely fill a certain shelf. 
Also, $S$ of the algebra books and $M$ of the geometry books would fill the same shelf. 
Finally, $E$ of the algebra books alone would fill this shelf. Given that $A, H, S, M, E$ are distinct positive integers, 
it follows that $E$ is
$\textbf{(A)}\ \frac{AM+SH}{M+H} \qquad \textbf{(B)}\ \frac{AM^2+SH^2}{M^2+H^2} \qquad \textbf{(C)}\ \frac{AH-SM}{M-H}\qquad \textbf{(D)}\ \frac{AM-SH}{M-H}\qquad \textbf{(E)}\ \frac{AM^2-SH^2}{M^2-H^2}$
Let $x$ and $y$ be the thicknesses of an algebra book and geometry book, respectively, and let $z$ be the length of the shelf. Then from the given information,
\[Ax + Hy = z\]
\[Sx + My = z\]
\[Ex = z.\] 
From the third equation, $x = z/E$. Substituting into the first two equations, we get
\[\frac{A}{E} z + Hy = z,\]
\[\frac{S}{E} z + My = z.\]
From the first equation,
\[Hy = z - \frac{A}{E} z = \frac{E - A}{E} z,\]
so
\[\frac{y}{z} = \frac{E - A}{EH}.\]
From the second equation,
\[My = z - \frac{S}{E} z = \frac{E - S}{E} z,\]
so
\[\frac{y}{z} = \frac{E - S}{ME}.\]
Hence,
\[\frac{E - A}{EH} = \frac{E - S}{ME}.\]
Multiplying both sides by $HME$, we get $ME - AM = HE - HS$. Then $(M - H)E = AM - HS$, so
\[E = \boxed{\frac{AM - HS}{M - H}}.\]
The answer is (D).