思路

将每一行的逆序存放在strs中，然后找strs中的所有line的相同前缀。

代码

#include <iostream>#include <vector>#include <string>#include <algorithm>usingnamespacestd;intmain(){intn;scanf("%d",&n);intenter;// the '\n' followed nscanf("%c",&enter);vector<string>strs;while(n--)// push back all the lines{chartmpc[266];intpos=0;while(scanf("%c",&tmpc[pos])&&tmpc[pos]!='\n')pos++;tmpc[pos]='\0';stringtmp=tmpc;reverse(tmp.begin(),tmp.end());strs.push_back(tmp);}intpos=0;boolflag=true;while(flag){chartarget=strs[0][pos];for(inti=1;i<strs.size();++i){if(pos<strs[i].size()){if(strs[i][pos]!=target){flag=false;break;}}else{flag=false;break;}//exceed the size of strs[i]}pos++;}stringresult=strs[0].substr(0,pos-1);reverse(result.begin(),result.end());if(result.size())cout<<result<<endl;elsecout<<"nai"<<endl;return0;}