• LA 3213 古老的密码


    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=15&page=show_problem&problem=1214

    题意:

    给定两个字符串,通过交换位置和一一映射判断能否使得两个字符串相同。

    思路:

    这题训练指南说得不准确,没有说到交换位置这一条件。既然可以交换位置,那我们只需要统计每个字符串中每个字母出现的次数,只要两个字符串中字母数相同并且出现次数也相同那就一定能转换成功。

     1 #include<iostream> 
     2 #include<algorithm>
     3 #include<string>
     4 #include<cstring>
     5 using namespace std;
     6 
     7 char s1[105], s2[105];
     8 int p1[26], p2[26];
     9 
    10 int main()
    11 {
    12     ios::sync_with_stdio(false);
    13     //freopen("D:\txt.txt", "r", stdin);
    14     while (cin >> s1)
    15     {
    16         cin >> s2;
    17         int len1 = strlen(s1);
    18         int len2 = strlen(s2);
    19         memset(p1, 0, sizeof(p1));
    20         memset(p2, 0, sizeof(p2));
    21         for (int i = 0; i < len1; i++)
    22             p1[s1[i] - 'A']++;
    23         for (int i = 0; i < len2; i++)
    24             p2[s2[i] - 'A']++;
    25         sort(p1, p1 + 26);
    26         sort(p2, p2 + 26);
    27         int flag = 1;
    28         for (int i = 0; i < 26;i++)
    29         if (p1[i] != p2[i])
    30         {
    31             flag = 0;
    32             break;
    33         }
    34         if (flag)  cout << "YES" << endl;
    35         else cout << "NO" << endl;
    36     }
    37 }
  • 相关阅读:
    meta属性
    博客
    概念术语
    装饰器与生成器
    Linux基础
    线程
    网络编程之socket
    网络编程之网络基础部分

    内置函数(max,min,zip)及文件处理
  • 原文地址:https://www.cnblogs.com/zyb993963526/p/6523497.html
Copyright © 2020-2023  润新知