~“持而盈之;揣而锐之” 题目: 试设计一个算法,求串S和串T的最大公约子串;要求: 1) 不使用高级函数 2) 可以求出首个最大公约串(最左) 3) 最好也可以求出所有最大公约串 贴上一个实现(返回最左公约串): string maxSub(char * s, char * t) { if(NULL==s || NULL==t)return ""; int max=0; string result=""; char * ss,*tt; //游标 //平行指针法 for(ss=s;*ss!='/0';ss+=1) { tt=t; //每次都从头起比对 while(*tt!=*ss && *tt!='/0')tt++; if(*tt!='/0')//现在,tt当前所指的和ss当前所指的相同;接下来看这种相同能保持多久。 { char * tempss=ss; int count=0; string tempResult=""; while(*tempss!='/0'&& *tt!='/0' && *tempss==*tt) { tempResult+=*tempss; count++; tempss++; tt++; } if(max<count) { max=count; result=tempResult; } } else continue;//ss所指字符没有在tt的任何位置出现,继续下一个循环 } return result; }