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, ...
随机推荐
- Linux centos 主机名颜色设置 和 别名设置
方便和乐趣写今天.至于为什么主机名颜色设置 和 别名设置放在一起写.这是因为他们的设置是在一个文件中..bashrc. .bashrc放在cd /root 这个文件夹下! 这个文件主要保存个人的一些个 ...
- docker study
Author:Hyphen 同步公布在个人博客中:http://yanheven.github.io/docker-study/ 參考章宇兄的开源项目学习ABC的方法来对docker进行简单的学习与分 ...
- Web项目
Eclipse部署Web项目(图文讲解) 讲解是在linux下完成的,但对windows系统,操作也是一样的,不要被吓到了 1.下载Eclipse
- SQL字符串转换为数组
/*一.按指定符号分割字符串,返回分割后的元素个数,方法很简单,就是看字符串中存在多少个分隔符号,然后再加一,就是要求的结果. -----rtrim(@str)去掉 @str右边的字符 ltrim(@ ...
- linux_shell_类似sql的orderby 取最大值
{} {} {} {} {} {} {} {} {} {} 需求场景 ,通过shell 筛选 每分钟的最大一条输出(最后一条) : .info | head
- Mysql sql声明召回2 --正则表达式
这里简单回顾mysql在过滤后的数据使用正则表达式时.主要利用每方法将样品,或添加一些简短的说明. 下面说说提前,使用正则表达式是用来过滤数据 REGEXPkeyword 基本字符相匹配: SELEC ...
- CSS3之重新定义鼠标右键
效果图: html: <div id="rightkey"> <ul> <li><img src="images/xmgl.pn ...
- 【转】Appium测试安卓Launcher以滑动窗体获得目标应用
原文地址:http://blog.csdn.net/zhubaitian/article/details/39755553 所谓Launcher,指的是安卓的桌面管理程序,所有的应用图标都放在laun ...
- Forms身份验证和基于Role的权限验证
Forms身份验证和基于Role的权限验证 从Membership到SimpleMembership再到ASP.NET Identity,ASP.NET每一次更换身份验证的组件,都让我更失望.Memb ...
- 在WAMPSERVER下增加多版本的PHP(PHP5.3,PHP5.4,PHP5.5)支持。
原文:在WAMPSERVER下增加多版本的PHP(PHP5.3,PHP5.4,PHP5.5)支持. WAMPServer可以让开发者在Windows系统下快速搭建WAMP环境,它支持多版本的Apach ...