SZU:A26 Anagram
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 ;
}
SZU:A26 Anagram的更多相关文章
- [LeetCode] Valid Anagram 验证变位词
Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = & ...
- Leetcode Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...
- LeetCode 242 Valid Anagram
Problem: Given two strings s and t, write a function to determine if t is an anagram of s. For examp ...
- 【09_242】Valid Anagram
Valid Anagram My Submissions Question Total Accepted: 43694 Total Submissions: 111615 Difficulty: Ea ...
- 【leetcode❤python】242. Valid Anagram
class Solution(object): def isAnagram(self, s, t): if sorted(list(s.lower()))==sorted(list ...
- 242. Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...
- (easy)LeetCode 242.Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...
- 【LeetCode】242 - Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...
- Java [Leetcode 242]Valid Anagram
题目描述: Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...
随机推荐
- [ Talk is Cheap Show me the CODE ] : jQuery Mobile页面布
[ Talk is Cheap Show me the CODE ] : jQuery Mobile页面布局 当我们专注地研究人类生活的空虚,并考虑荣华富贵空幻无常时,或许我们正在阿谀逢迎自己懒惰的天 ...
- sgu 194 被动散热器具有最大流量的上限和下限(最大流量模板dinic加上优化)
模板类型的题详细參考国家集训队论文:http://wenku.baidu.com/view/0f3b691c59eef8c75fbfb35c.html 參考博客:http://blog.csdn.ne ...
- maven+hudson构建集成测试平台
1.下载hudson.war.2.命令行运行:java -jar hudson.war --httpPort=8070 -Dorg.eclipse.jetty.util.URI.charset=GB ...
- hdu 4864 Task(贪婪啊)
主题链接:pid=4864">http://acm.hdu.edu.cn/showproblem.php?pid=4864 Task Time Limit: 4000/2000 MS ...
- winform屏幕截图
原文:winform屏幕截图 屏幕截图是一个比较常用的功能,在项目中出现的比例也比较高,至少我做过的每个项目都有屏幕截图这个功能,从全屏截图到区域截图都有出现过.当然区域截图已然包含了全屏截图. 全屏 ...
- 浏览器扩展系列————在WPF中定制WebBrowser快捷菜单
原文:浏览器扩展系列----在WPF中定制WebBrowser快捷菜单 关于如何定制菜单可以参考codeproject上的这篇文章:http://www.codeproject.com/KB/book ...
- Linux环境编程相关的文章
Linux环境编程相关的文章 好几年没有接触Linux环境下编程了,好多东西都有点生疏了.趁着现在有空打算把相关的一些技能重拾一下,顺手写一些相关的文章加深印象. 因为不是写书,也受到许多外部因素限制 ...
- 设置CentOS开机连接网络 Centos 开机启动网卡的设置方法
我们开机网卡不能启动所以只能使用 ifup eth0 (eth0指的网卡) 来启动了,但是每一次都这样感觉不方便希望开机自动启动网卡 后来百度搜索了一下发现可以通过修改网卡( ifcfg-eth0)参 ...
- linux 下一个 jira-6.3.6 组态 皴 翻译 迁移数据库
每一个版本号翻译包下载 https://translations.atlassian.com/dashboard/download jira下载地址 https://www.atlassian.c ...
- vim打开出现的文档^M什么
网上公开的一些代码,发现里面多^M符号.这是什么? 我搜索^M没有效果,这应该是一个特殊的控制字符.找换行的结果是不.在每一行的末尾是回车,代替它周围包裹,对于由线定义不同的编码系统是不一样的. li ...