美文网首页
字符串的快速比对

字符串的快速比对

作者: 懒生活 | 来源:发表于2020-09-21 22:49 被阅读0次
    typedef unsigned long long bit;
    bit tobit(char* pchar)
    {
        bit ret = 0;
        while (*pchar != 0)
        {
            ret |= bit((*pchar - 'a' + 1));
            ret = ret << 5;
            pchar++;
        }
        return ret;
    }
    
    void tochar(bit val, char* pchar)
    {
        bit mask = 0x1f;
        int j = 0;
        for (int i = 10; i >= 0; i--)
        {
            bit curmask = mask << 5 * i;
            char secval = (val & curmask) >> 5 * i;
            if (secval == 0)
            {
                continue;
            }
            else
            {
                pchar[j] = secval - 1 + 'a';
                j++;
            }
        }
        pchar[j] = 0;
    }
    
    char getchar(bit val, int idx)
    {
        int len = getlen(val);
        //idx 0 ==> len_th
        int realidx = len - idx;
        bit mask = 0x1f;
        bit curmask = mask << (realidx-1) * 5;
        char secval = (val & curmask) >> (realidx-1) * 5;
        return (secval - 1 + 'a');
    }
    bool isOneCharDiff(bit val1, bit val2)
    {
        bit temp = val1 ^ val2;
        bit mask = 0x1f;
        int difcnt = 0;
        for (int i = 0; i < 10; i++)
        {
            if ((temp & mask) != 0)
            {
                difcnt++;
            }
            mask = mask << 5;
        }
        if (difcnt == 1)
            return true;
        else
            return false;
    }
    
    

    相关文章

      网友评论

          本文标题:字符串的快速比对

          本文链接:https://www.haomeiwen.com/subject/iyzbyktx.html