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, , 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 ;
}

最新文章

  1. 60行JavaScript俄罗斯方块
  2. 终于吧Appserv搞通了
  3. Android 框架修炼-自己开发高效异步图片加载框架
  4. listview必须设置数据适配器才能显示出来
  5. O(n)线性时间找第K大,中位数
  6. SSM-SpringMVC-31:SpringMVC中利用hibernate-validator做后台校验
  7. ios设置音乐audio自动播放
  8. 数据结构树之AVL树(平衡二叉树)
  9. LVS简单介绍
  10. 解析/proc/net/dev
  11. JsonWebToken Demo(转)
  12. MSTSC 3389 端口修改
  13. KVM -&gt; 虚拟机管理&amp;console登录_02
  14. day 0314函数的进阶
  15. 1-2-编译U-boot
  16. svn 提交数据
  17. ExtJS 4.2 教程-07:Ext.Direct
  18. 2. KNN和KdTree算法实现
  19. 第三百一十二节,Django框架,Cookie
  20. Excel VBA入门(八)单元格边框

热门文章

  1. SpringMVC传参
  2. POJ3690 Constellations 【KMP】
  3. [译文]c# /.Net 技巧: ToDictionary() and ToList()
  4. java_ log4j的基本配置参数
  5. 基于.NET MVC的高性能IOC插件化架构
  6. PHP的垃圾回收机制详解
  7. CSS3+HTML5特效2 - 翻转效果
  8. ArcGIS API for Silverlight 编辑Geometry
  9. 在线Youtube视频下载,修改文本,剪切制作动画的最新方法
  10. js对象字面量