#include #include #include #include #include #include #include #include using namespace std; #define myforeach(c,it,ty) for(ty::iterator it=c.begin() ; it!=c.end() ; ++it ) typedef set charset; struct frame { int x[2]; int y[2]; charset lower; frame(){}; frame(int x0, int y0){ x[0]=x[1]=x0; y[0]=y[1]=y0; } void add(int x1, int y1){ if ( x1 frames; typedef vector field; void search(frames &fra, const string &his) { myforeach(fra, cur, frames){ if ( find(his.begin(), his.end(), cur->first)!=his.end() ) continue; charset &low = cur->second.lower; charset::iterator cit=low.begin(); for ( ; cit!=low.end() && find(his.begin(), his.end(),*cit)!=his.end() ; ++cit ) ; if ( cit==low.end() ) { string nexthis = his+cur->first; if ( nexthis.size() == fra.size() ) { cout<>height>>width && height ) { field fl; for ( int i=0 ; i>str; fl.push_back(str); } frames fra; for ( int i=0 ; ifirst; frame &f=it->second; for ( int side=0 ; side<2 ; side++ ) { for ( int x=f.x[0] ; x<=f.x[1] ; x++ ) { char here=fl[x][f.y[side]]; if ( here!=my_c ) { fra[here].lower.insert(my_c); } } for ( int y=f.y[0] ; y<=f.y[1] ; y++ ) { char here=fl[f.x[side]][y]; if ( here!=my_c ) { fra[here].lower.insert(my_c); } } } } string str; search(fra,str); cout<