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, ...
随机推荐
- Parse 和 Swift 搭建一个像 Instagram
如何用 Parse 和 Swift 搭建一个像 Instagram 那样的应用? [编者按]本篇文章作者是Reinder de Vries,既是一名企业家,也是优秀的程序员,发表多篇应用程序的博客 ...
- JMeter怎么在get URL请求、POST请求中添加动态参数用于服务器段安全验证
从前一个页面(含有服务器段返回给客户端的参数,用于在下一次请求时验证)中添加后置处理器中的Regular Expression Extractor,使用正则表达式对参数进行提取. 在用到这些变量时可以 ...
- 我的MYSQL学习心得(二)
原文:我的MYSQL学习心得(二) 我的MYSQL学习心得(二) 我的MYSQL学习心得(一) 我的MYSQL学习心得(三) 我的MYSQL学习心得(四) 我的MYSQL学习心得(五) 我的MYSQL ...
- poj1251--Kruskal
/* * poj1251-- Kruskal * date 2014/7/15 * state AC */ #include <iostream> #include <algorit ...
- webservice发送字符串
假设只是发送一个字符串client,这是很easy,只需要输入xfire包,编写接口,编写的实现方法.变化. 假设你要传输的数组或自定义类.到用于接口准备的需要agexis文件.更复杂. 尝试传输这些 ...
- A + B Again 2057 有符号的64进位的运算
Problem Description There must be many A + B problems in our HDOJ , now a new one is coming.Give you ...
- 【高德地图API】汇润做爱地图技术大揭秘
原文:[高德地图API]汇润做爱地图技术大揭秘 昨日收到了高德地图微信公众号的消息推送,说有[一大波免费情趣用品正在袭来],点进去看了一眼,说一个电商公司(估计是卖情趣用品的)用高德云图制作了一张可以 ...
- 教你一步一步部署.net免费空间OpenShift系列之四------绑定域名、使用CDN加速
很抱歉这几天没有时间,有人问我怎么绑定域名的问题也没有答复,下面进入正题,惊闻ASP.Net要开源了,难道.Net春天要来了?不废话,上回书说,部署完毕ASP.Net网站后,直接访问不能访问(嗯,众所 ...
- Linux下df与du两个命令的差别?
Linux下df与du两个命令的差别? 一.df显示文件系统的使用情况,与du比較,就是更全盘化. 最经常使用的就是 df -T,显示文件系统的使用情况并显示文件系统的类型. 举比例如以下: [roo ...
- WebBrowser控件的简单应用2
原文:WebBrowser控件的简单应用2 第一个简单应用里面讲述的是如何模拟调用当前网页的元素的事件或者赋值/取值.这次的应用讲述的是1:如何处理弹出新页面的事件(总是在我的浏览器里面现实新页面)2 ...