SZU:A26 Anagram

时间:2024-01-17 08:41:14

Judge Info

  • Memory Limit: 32768KB
  • Case Time Limit: 10000MS
  • Time Limit: 10000MS
  • Judger: Normal

Description

An anagram is formed by rearranging the letters of a word. You are given a string, please find out if it is an anagram of a word or not. No word will have have more than 50 characters.

Input

The input will consist of a word on one line. The following line contains a number, SZU:A26 Anagram, of strings to be tested.

Output

For each test string, if the test string is identical to the source string, output 'IDENTICAL', if it is an anagram, output 'ANAGRAM' otherwise output 'NOT AN ANAGRAM', in a single line.

Sample Input

cares
5
scare
races
cares
another
acres

Sample Output

ANAGRAM
ANAGRAM
IDENTICAL
NOT AN ANAGRAM
ANAGRAM

解题思路:字符串数组排序,但是我的方法并不好,只是勉强解出来而已。不过学会了使用qsort函数。

 #include <stdio.h>
#include <string.h>
char A[];
char B[];
char C[]; void swap(char *a,char *b){
char t;
t=*a;
*a=*b;
*b=t;
} int main() {
scanf("%s",A);
int n,flag,i,j;
scanf("%d",&n);
for (i=;i<strlen(A);++i){
C[i]=A[i];
}
while (n--) { scanf("%s",B);
flag=;
for (i=;i<strlen(A);++i) {
if(A[i]!=B[i])
flag=;
}
if(flag==){printf("IDENTICAL\n"); continue;}
for (i=;i<strlen(C)-;++i) {
for (j=i+;j<strlen(C);++j) {
if(B[i]>B[j])
swap(&B[i],&B[j]);
if(C[i]>C[j])
swap(&C[i],&C[j]);
}
} for (i=;i<strlen(A);++i) {
if(C[i]!=B[i])
flag=;
}
if(flag==){printf("ANAGRAM\n"); continue;}
else printf("NOT AN ANAGRAM\n");
}
}

大神解法:

 #include<stdio.h>
#include<stdlib.h>
#include<string.h> char S[]; int cmp(const void *a,const void *b)
{
return *(char *)a-*(char *)b;
} int main()
{
int n,i,len1,len2;
char str[],temp[];
scanf("%s",S);
strcpy(temp,S);
len1=strlen(S);
qsort(S,len1,sizeof(char),cmp);
scanf("%d",&n);
for(i=;i<n;i++)
{
memset(str,,sizeof(str));
scanf("%s",str);
len2=strlen(str);
if(len2!=len1)
{
printf("NOT AN ANAGRAM\n");
continue;
}
if(==strcmp(str,temp))
{
printf("IDENTICAL\n");
continue;
}
else
{
qsort(str,len2,sizeof(char),cmp);
if(==strcmp(S,str))
printf("ANAGRAM\n");
else
printf("NOT AN ANAGRAM\n");
}
}
return ;
}