  Intrinsic Josephson-junction stacks realized in high-temperature
superconductors provide a very attractive base for developing coherent sources
of electromagnetic radiation in the terahertz frequency range. A promising way
to synchronize phase oscillations in all the junctions is to excite an internal
cavity resonance. We demonstrate that this resonance promotes the formation of
an alternating coherent state, in which the system spontaneously splits into
two subsystems with different phase-oscillation patterns. There is a static
phase shift between the oscillations in the two subsystems which changes from 0
to $2\pi$ in a narrow region near the stack center. The oscillating electric
and magnetic fields are almost homogeneous in all the junctions. The formation
of this state promotes efficient pumping of the energy into the cavity
resonance leading to strong resonance features in the current-voltage
dependence.
