A new computational approach for the efficient docking of flexible ligands in a rigid protein is presented. It exploits the binding modes of functional groups determined by an exhaustive search with solvation. The search in ligand conformational space is performed by a genetic algorithm whose scoring function approximates steric effects and intermolecular hydrogen bonds. Ligand conformations generated by the genetic algorithm are docked in the protein binding site by optimizing the fit of their fragments to optimal positions of chemically related functional groups. We show that the use of optimal binding modes of molecular fragments allows to dock known inhibitors with about ten rotatable bonds in the active site of the uncomplexed and complexed conformations of thrombin and HIV-1 protease.