Description
time limit per test : 1 secondtimelimitpertest:1second
memory limit per test : 128 megabytesmemorylimitpertest:128megabytes
给出一个由字符 ‘b’,‘d’,‘p’,‘q’ 构成的字符串 S,判断 S 是不是镜像字符串。镜像字符串指 S 经过以下操作后得到的字符串仍为 S :
- 反转 SS ((例:“bdpq” ⇒ “qpdb” ))
- 将 ‘b’ 改为 ‘d’,‘d’ 改为 ‘b’,’p‘ 改为 ‘q’,’q‘ 改为 ’p‘
Input
一行一个字符串 S(len(S)≤10^3)。
Output
若SS为镜像字符串,输出 YES,否则输出 NO。
Sample Input 1
pbdq
Sample Output 1
YES
Sample Input 2
ppqb
Sample Output 2
NO
思路:
1.先判断字符串长度是否是偶数,不然就不能是镜像字符串
2.找到规律,在下列代码有。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(void){
int i,m,t;
char *a,*b;
a=(char *)malloc(sizeof(char) *100);
b=(char *)malloc(sizeof(char) *100);
while(scanf("%s",a) != EOF){
m=strlen(a);
t=1;
if(m%2==1){
t=0;
}
else{
for(i=0;i<m/2;i++){ //判断对应项的字符是否对应
if(a[i]=='b'&&a[m-1-i]!='d'){
t=0;
break;
}
if(a[i]=='d'&&a[m-1-i]!='b'){
t=0;
break;
}
if(a[i]=='q'&&a[m-1-i]!='p'){
t=0;
break;
}
if(a[i]=='p'&&a[m-1-i]!='q'){
t=0;
break;
}
}
}
if(t==1){
printf("YES\n");
}
else{
printf("NO\n");
}
}
}
&spm=1001.2101.3001.5002&articleId=109710900&d=1&t=3&u=3cf472a05d1f45918d3f55748a2ee7ef)
772

被折叠的 条评论
为什么被折叠?



