码迷,mamicode.com
首页 > 其他好文 > 详细

[算法]判断兄弟单词

时间:2014-07-07 20:57:31      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   数据   os   for   

一个单词单词字母交换,可得另一个单词,如army->mary,成为兄弟单词。提供一个单词,在字典中找到它的兄弟。描述数据结构和查询过程。

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
void add(unsigned int count[],char c){
    if(c <= Z){
        ++count[c - A];
    }
    else{
        ++count[c - a];
    }
    
}
bool isBrother(const string& wa,const string& wb){
    if(wa.length() != wb.length()){
        return false;
    }
    unsigned int counta[26],countb[26];
    memset(counta,0,26*4);
    memset(countb,0,26*4);
    for(int i = 0; i < wa.length(); ++i){
        add(counta,wa.at(i));
        add(countb,wb.at(i));
    }
    for(int i = 0; i < 26; ++i){
        if(counta[i]!=countb[i]){
            return false;
        }
    }
    return true;
} 

 

[算法]判断兄弟单词,布布扣,bubuko.com

[算法]判断兄弟单词

标签:style   blog   color   数据   os   for   

原文地址:http://www.cnblogs.com/zhengyuhong/p/3813157.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!