一天一道算法题--5.28--字符串的映射

感谢  微信平台: 一天一道算法题    ---------------- 每天多一点进步---------------

这题 根据题意  网上终于找到个 相似的题目

                       戳我

原题 给的实在太长了  我给你简短翻译一下:

给定两个长度均为N的字符串( N<=100) 判断它们之间的26个字母能否一一对应 如abb和cdd可以相互对应  方法是a->c  b->d

要是相同 输出YES 否则 输出NO

 

//先去次饭了 回来再写下去。。。。

 

go on。。。

讲下这题的思路:

既然是字母一一对应的 那么如果是YES的情况下 那么相对应的字母的个数必然是相等的 想到这里 这题就不难了  就是怎么将字符串中每个字母出现的次数保存下来

这边 注意的是 不能忘记了排序   你可能A中 保存了3个a 2个b  B中保持了2个c 3个d 要是不进行排序 可能导致2和3 进行比较 就得出了错误的结论

 

这边它提供了 用 map的做法 看上去 的确 高大上 而且很舒服

 1 #include <iostream>
 2 #include <map>
 3 #include <string>
 4 #include <cstdio>
 5 #include <cstring>
 6 #include <algorithm>
 7 using namespace std;
 8 
 9 string str1 , str2;
10 int cntA[110];
11 int cntB[110];
12 int main()
13 {
14     int len , aLen , bLen;
15     int i;
16     while( cin>>str1>>str2 )
17     {
18         bool flag = true;
19         memset( cntA , 0 , sizeof(cntA) );
20         memset( cntB , 0 , sizeof(cntB) );
21         map<char , int> strA;
22         map<char , int>::iterator itA;
23         map<char , int> strB;
24         map<char , int>::iterator itB;
25         len = str1.length();
26         for( i = 0 ; i<len ; i++ )
27         {
28             strA[ str1[i] ]++;
29             strB[ str2[i] ]++;
30         }
31         aLen = strA.size();
32         bLen = strB.size();
33         if( aLen!=bLen )
34         {
35             printf( "NO\n" );
36         }
37         else
38         {
39             i = 0;
40             for( itA = strA.begin() ; itA!=strA.end() ; itA++ )
41             {
42                 cntA[ i++ ] = itA->second;
43             }
44             i = 0;
45             for( itB = strB.begin() ; itBstrB.end() ; itB++ )
46             {
47                 cntB[ i++ ] = itB->second;
48             }
49             sort( cntA , cntA+aLen );
50             sort( cntB , cntB+bLen );
51             for( i = 0 ; i<aLen ; i++ )
52             {
53                 if( cntA[i]!=cntB[i] )
54                 {
55                     flag = false;
56                     break;
57                 }
58             }
59             printf( "%s\n",flag?"YES":"NO" );
60         }
61     }
62     return 0;
63 }
View Code

 

我自己写了个不用map实现的 代码短了点 但感受还是用map看上去 厉害点。。。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 char strA[110] , strB[110];
 7 int cntA[110] , cntB[110];
 8 
 9 int main()
10 {
11     int i;
12     bool flag;
13     while( ~scanf("%s %s",strA,strB) )
14     {
15         flag = true;
16         memset( cntA , 0 , sizeof(cntA) );
17         memset( cntB , 0 , sizeof(cntB) );
18         for( i = 0 ; strA[i]!='\0' ; i++ )
19         {
20             cntA[ 'Z'-strA[i] ]++;
21             cntB[ 'Z'-strB[i] ]++;
22         }
23         sort( cntA , cntA+26 );
24         sort( cntB , cntB+26 );
25         for( i=0 ; i<=25 ; i++ )
26         {
27             if( cntA[i]!=cntB[i] )
28             {
29                 flag = false;
30                 break;
31             }
32         }
33         printf( "%s\n",flag?"YES":"NO" );
34     }
35     return 0;
36 }
View Code

 

最近 要作web   又要应付 高数 ....

有个妹子 陪伴就好了

new 一个出来?

可以不 delete 吗..................

 

 

 

 

转载于:https://www.cnblogs.com/radical/p/3757725.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值