SZU:A26 Anagram
2024-09-13 11:38:27
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 ;
}
最新文章
- 60行JavaScript俄罗斯方块
- 终于吧Appserv搞通了
- Android 框架修炼-自己开发高效异步图片加载框架
- listview必须设置数据适配器才能显示出来
- O(n)线性时间找第K大,中位数
- SSM-SpringMVC-31:SpringMVC中利用hibernate-validator做后台校验
- ios设置音乐audio自动播放
- 数据结构树之AVL树(平衡二叉树)
- LVS简单介绍
- 解析/proc/net/dev
- JsonWebToken Demo(转)
- MSTSC 3389 端口修改
- KVM ->; 虚拟机管理&;console登录_02
- day 0314函数的进阶
- 1-2-编译U-boot
- svn 提交数据
- ExtJS 4.2 教程-07:Ext.Direct
- 2. KNN和KdTree算法实现
- 第三百一十二节,Django框架,Cookie
- Excel VBA入门(八)单元格边框
热门文章
- SpringMVC传参
- POJ3690 Constellations 【KMP】
- [译文]c# /.Net 技巧: ToDictionary() and ToList()
- java_ log4j的基本配置参数
- 基于.NET MVC的高性能IOC插件化架构
- PHP的垃圾回收机制详解
- CSS3+HTML5特效2 - 翻转效果
- ArcGIS API for Silverlight 编辑Geometry
- 在线Youtube视频下载,修改文本,剪切制作动画的最新方法
- js对象字面量