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

寻找兄弟字符串

时间:2017-05-16 10:53:25      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:sys   and   i++   pac   匹配   ros   nbsp   ios   ++   

/* 
如果两个字符串的字符一样,但是顺序不一样,被认为是兄弟字符串, 
问如何在迅速匹配兄弟字符串(如,bad和adb就是兄弟字符串)。 
*/  
#include <iostream>  
#include<cstring>
using namespace std;  
  
int isBroStr(char *str1, char *str2)  
{  
    int a[26 * 2] = {0};  
    int i, strLen;  
      
    if (!str1 && !str2)  
        return 1;  
    else if (!str1 || !str2)  
        return 0;  
    else  
    {  
        if(strlen(str1) != strlen(str2))  
            return 0;  
        strLen = strlen(str1);  
        for(i = 0; i < strLen; i++)  
        {  
            ++a[str1[i] - A];  
            --a[str2[i] - A];  
        }  
        for(i = 0; i < 26 * 2; i++)  
            if (a[i])  
                return 0;  
        return 1;  
    }          
}  
  
int main()  
{  
    char *str1 = "asdfaabAAB";  
    char *str2 = "asdfAABaab";  
    if (isBroStr(str1, str2))  
        cout << " String 1 and String 2 are brothers!" << endl;  
    else  
        cout << " String 1 and String 2 are not brothers!" << endl;  
    system("PAUSE");  
    return 0;  
}  

 

寻找兄弟字符串

标签:sys   and   i++   pac   匹配   ros   nbsp   ios   ++   

原文地址:http://www.cnblogs.com/wft1990/p/6859837.html

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