您的位置:首页 > 编程语言 > C语言/C++

PAT程序设计考题——甲级1077(Kuchiguse ) C++实现

2017-07-12 19:30 603 查看
点击打开pat链接

#include<iostream>

#include<math.h>

#include<algorithm>

#include<queue>

#include<map>

#include<set>

#include<stack>

#include<string>

#include<vector>

using namespace std;

#define INF 100000000

#define maxn 100010

vector<string> sto;

 string temp;

void reverse()//用参数赋值的话一定要返回

{

 int m=temp.size()-1;

 int n=0;

 for(;n<=m;n++,m--)

 swap(temp[m],temp
);

 }

int main()//这一题为了判断最长公共后缀 转置很重要

{

 int num;

 cin>>num;

 cin.ignore();

 int len=INF;int k;

 for(int i=0;i<num;i++)

 {

 getline(cin,temp);

 reverse();

 sto.push_back(temp); 

 if(len>temp.size()) len=temp.size();//记录最短长度

 }

 bool flag=false;

 for(k=0;k<len;k++)

 {

  for(int j=0;j<num-1;j++)

  { //cout<<sto[j][k]<<","<<sto[j+1][k]<<endl;

   if(sto[j][k]!=sto[j+1][k]) {

    flag=true;

    break;

   }

  }

  if(flag) break;

 }

 //for(int i=0;i<num;i++)

// cout<<sto[i]<<endl;

 if(k==0) cout<<"nai";

 else{k--;

 for(int qw=k;qw>=0;qw--)

 cout<<sto[0][qw];

 }

 

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: