ACM问题 我的代码哪里错了?? http://acm.swjtu.edu.cn/JudgeOnline/showproblem?problem_id=1356
http://acm.swjtu.edu.cn/JudgeOnline/showproblem?problem_id=1356我的代码:Astringyisapermut...
http://acm.swjtu.edu.cn/JudgeOnline/showproblem?problem_id=1356
我的代码:
A string y is a permutation of a string x if y can be obtained by rearranging the characters of x. Write a program which repeatedly inputs pairs of strings and decides whether the second string is a permutation of the first string.
Input
A string is the concatenation of consecutive nonempty lines of input. A pair consists of a nonempty line (containing only hyphens), followed by the first string, followed by an empty line, followed by the second string, followed by an empty line. The standard input file contains one or more pairs.
Output
for each pair of strings in the standard input file, determine whether the second string is a permutation of the first string, and output a message as shown in the sample output.
Sample Input
-----
Oh, my goodness
multiple lines!
yg doo, shOmens emlusleip
!tlin
-----
perplexed
exdrelper
Sample Output
-----
The second string is a permutation of the first
-----
The second string is not a permutation of the first
Source
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
int h1,h2,i,j;
int lettera[257],letterb[257],sign;
char s[100],a[100001],b[100001];
// fgets(buf,sizeof(s),stdin);// do not use gets()
//先把所有输入都读完,再去做进一步处理。
//读取代码:
while(gets(s)) // 如果只输入两次就运行出结果 就只gets两次就行
{
char *aBuf;
memset(a,0,sizeof(a)); // a是前面定义的数组
aBuf=a;
while(gets(aBuf))
{
if(strcmp(aBuf,"")==0)
break;
aBuf+=strlen(aBuf); //这样,所有字符都被读进了a,并且所有换行符都被去除了。
}
char *bBuf;
memset(b,0,sizeof(b)); // a是前面定义的数组
bBuf=b;
while(gets(bBuf))
{
if(strcmp(bBuf,"")==0)
break;
bBuf+=strlen(bBuf); //这样,所有字符都被读进了b,并且所有换行符都被去除了。
}
// printf("a: %s\n",a);
// printf("b: %s\n",b);
memset(lettera,0,sizeof(lettera));
memset(letterb,0,sizeof(letterb));
h1=strlen(a); h2=strlen(b);
for(i=0;i<h1;i++)
lettera[a[i]]++;
for(i=0;i<h2;i++)
letterb[b[i]]++;
sign=0;
for(i=0;i<257;i++)
{
if(lettera[i]!=letterb[i])
{
sign=1;
break;
}
}
if(sign==1)
{
cout<<"-----"<<endl;
cout<<"The second string is a not permutation of the first"<<endl;
}
else
{
cout<<"-----"<<endl;
cout<<"The second string is a permutation of the first"<<endl;
}
}
return 0;
} 展开
我的代码:
A string y is a permutation of a string x if y can be obtained by rearranging the characters of x. Write a program which repeatedly inputs pairs of strings and decides whether the second string is a permutation of the first string.
Input
A string is the concatenation of consecutive nonempty lines of input. A pair consists of a nonempty line (containing only hyphens), followed by the first string, followed by an empty line, followed by the second string, followed by an empty line. The standard input file contains one or more pairs.
Output
for each pair of strings in the standard input file, determine whether the second string is a permutation of the first string, and output a message as shown in the sample output.
Sample Input
-----
Oh, my goodness
multiple lines!
yg doo, shOmens emlusleip
!tlin
-----
perplexed
exdrelper
Sample Output
-----
The second string is a permutation of the first
-----
The second string is not a permutation of the first
Source
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
int h1,h2,i,j;
int lettera[257],letterb[257],sign;
char s[100],a[100001],b[100001];
// fgets(buf,sizeof(s),stdin);// do not use gets()
//先把所有输入都读完,再去做进一步处理。
//读取代码:
while(gets(s)) // 如果只输入两次就运行出结果 就只gets两次就行
{
char *aBuf;
memset(a,0,sizeof(a)); // a是前面定义的数组
aBuf=a;
while(gets(aBuf))
{
if(strcmp(aBuf,"")==0)
break;
aBuf+=strlen(aBuf); //这样,所有字符都被读进了a,并且所有换行符都被去除了。
}
char *bBuf;
memset(b,0,sizeof(b)); // a是前面定义的数组
bBuf=b;
while(gets(bBuf))
{
if(strcmp(bBuf,"")==0)
break;
bBuf+=strlen(bBuf); //这样,所有字符都被读进了b,并且所有换行符都被去除了。
}
// printf("a: %s\n",a);
// printf("b: %s\n",b);
memset(lettera,0,sizeof(lettera));
memset(letterb,0,sizeof(letterb));
h1=strlen(a); h2=strlen(b);
for(i=0;i<h1;i++)
lettera[a[i]]++;
for(i=0;i<h2;i++)
letterb[b[i]]++;
sign=0;
for(i=0;i<257;i++)
{
if(lettera[i]!=letterb[i])
{
sign=1;
break;
}
}
if(sign==1)
{
cout<<"-----"<<endl;
cout<<"The second string is a not permutation of the first"<<endl;
}
else
{
cout<<"-----"<<endl;
cout<<"The second string is a permutation of the first"<<endl;
}
}
return 0;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询