Career Cup 1-4

/*******************************************************

    Write a method to decide if two strings are anagrams or not.
    也就是说纯看字母出现次数咯?与位置无关,字符出现类型和次数相同则为anagram.
    i.e. transpose letters字母完全不变,把字母位置进行调换,得到新的意思.

  Note:

  Mod Time: 4.20, 2012

  Copyright: Ben

  *******************************************************/


#include<stdio.h>
#include<stdlib.h>

#define STRING_MAX_LEN            10000

int main(void)
{
    bool anagram;
    char s1[STRING_MAX_LEN], s2[STRING_MAX_LEN];
    char ch;
    int StLen1 = 0, StLen2 = 0;
    int BitMap1[128], BitMap2[128];
    int i;

    //initialize
    for(i = 0; i < 128; i++)
    {
        BitMap1[i] = 0;
        BitMap2[i] = 0;
    }

    freopen("String1.txt", "r", stdin);

    while((ch = fgetc(stdin)) != EOF)
    {
        s1[StLen1++] = ch;
    }
    s1[StLen1] = '\0';    //whole string1 is read.

    freopen("String2.txt", "r", stdin);

    while((ch = fgetc(stdin)) != EOF)
    {
        s2[StLen2++] = ch;
    }
    s2[StLen2] = '\0';    //whole string2 is read.

    //so far the 2 strings have been read from separate files.
#if 0
    if(StLen1 != StLen2)
    {
        anagram = false;
    }
    else
#endif
    {
        //read and fill bitmaps. Maybe this can also be done in the reading process.
        for(i = 0; i < StLen1; i++)
        {
            //if the char is not btw 0~127(ascii), DANGER of access violation!
            BitMap1[s1[i]]++;
            BitMap2[s2[i]]++;
        }

        anagram = true;
        //FOR_LOOP:
        for(i = 0; i < 128; i++)
        {
            if(BitMap1[i] != BitMap2[i])
            {
                //if((i != 10) && (i != 13) && (i != 32))
                {
                    anagram = false;
                    break;
                    //break FOR_LOOP;
                }
            }
        }
    }

    printf((anagram) ? ("They are anagram!\n") : ("Not anagrams!\n"));

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值