在线OJ为了保证测评的公正和系统的安全,删除了部分函数的接口,如(gets())
PAT A1077题
1077 Kuchiguse (20 分)
The Japanese language is notorious for its sentence ending particles. Personal preference of such particles can be considered as a reflection of the speaker’s personality. Such a preference is called “Kuchiguse” and is often exaggerated artistically in Anime and Manga. For example, the artificial sentence ending particle “nyan~” is often used as a stereotype for characters with a cat-like personality:
Itai nyan~ (It hurts, nyan~)
Ninjin wa iyada nyan~ (I hate carrots, nyan~)
Now given a few lines spoken by the same character, can you find her Kuchiguse?
Input Specification:
Each input file contains one test case. For each case, the first line is an integer N (2≤N≤100). Following are N file lines of 0~256 (inclusive) characters in length, each representing a character’s spoken line. The spoken lines are case sensitive.
Output Specification:
For each test case, print in one line the kuchiguse of the character, i.e., the longest common suffix of all N lines. If there is no such suffix, write nai.
Sample Input 1:
3
Itai nyan~
Ninjin wa iyadanyan~
uhhh nyan~
Sample Output 1:
nyan~
Sample Input 2:
3
Itai!
Ninjinnwaiyada T_T
T_T
Sample Output 2:
nai
error: ‘gets’ was not declared in this scope gets(s[i]);
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{
int n,minlen=999,ans=0;
int i,j;
char s[105][260];
cin>>n;
getchar();//避免换行符被当作字符存入字符数组
char tem;
int len;
for(i=0;i<n;i++)
{
gets(s[i]); //PAT取消gets();导致此处会报错
len=strlen(s[i]);
minlen=minlen<len?minlen:len;
for(j=0;j<len/2;j++)
{
tem=s[i][j];
s[i][j]=s[i][len-j-1];
s[i][len-j-1]=tem;
}
}
for(i=0;i<minlen;i++)
{
bool temp=true;
for(j=1;j<n;j++)
{
if(s[0][i]!=s[j][i])
{
temp=false;
break;
}
}
if(temp)
ans++;
else
break;
}
if(ans)
for(i=ans-1;i>=0;i--)
cout<<s[0][i];
else
cout<<"nai";
return 0;
}
正解即将
gets(s[i]);
换成
cin.getline(s[i],257);
这里长度要设成257,即比最大的多一位,否则有一个数据过不去,因为实际输入长度为257-1.
关于cin,cin.get,cin.getline,getline的区别详细讲解(超级详细)
这题不难,具体解法和思想csdn上很好找,这里主要是想说PAT的编译器不支持gets();函数,具体还有哪些不支持的也可查