• C. awoo‘s Favorite Problem--Educational Codeforces Round 130 (Rated for Div. 2)


    Problem - 1697C - Codeforces

    C. awoo's Favorite Problem

    time limit per test

    2 seconds

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    You are given two strings ss and tt, both of length nn. Each character in both string is 'a', 'b' or 'c'.

    In one move, you can perform one of the following actions:

    • choose an occurrence of "ab" in ss and replace it with "ba";
    • choose an occurrence of "bc" in ss and replace it with "cb".

    You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string ss to make it equal to string tt?

    Input

    The first line contains a single integer qq (1≤q≤1041≤q≤104) — the number of testcases.

    The first line of each testcase contains a single integer nn (1≤n≤1051≤n≤105) — the length of strings ss and tt.

    The second line contains string ss of length nn. Each character is 'a', 'b' or 'c'.

    The third line contains string tt of length nn. Each character is 'a', 'b' or 'c'.

    The sum of nn over all testcases doesn't exceed 105105.

    Output

    For each testcase, print "YES" if you can change string ss to make it equal to string tt by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".

    Example

    input

    Copy

    5
    3
    cab
    cab
    1
    a
    b
    6
    abbabc
    bbaacb
    10
    bcaabababc
    cbbababaac
    2
    ba
    ab
    

    output

    Copy

    YES
    NO
    YES
    YES
    NO
    

    =========================================================================

    ab ->ba

    bc->cb

    第一个置换只能有两个作用,那就是aaaab把b放到前面,abbbb把a放在后面

    第二个置换同理

    很自然的贪心。我们从末尾往前扫描,如果相等那么继续,否则,如果b对a,往前找a, c对b,往前找b,找不到或者不是这种配对,那么就无解。做多了感觉瞬间来。

    1. # include
    2. # include
    3. # include
    4. # include
    5. # include
    6. # define mod 1000000007
    7. using namespace std;
    8. typedef long long int ll;
    9. int main()
    10. {
    11. int t;
    12. cin>>t;
    13. while(t--)
    14. {
    15. int n;
    16. cin>>n;
    17. string s,t;
    18. cin>>s>>t;
    19. int flag=0;
    20. for(int i=n-1;i>=0;i--)
    21. {
    22. if(s[i]==t[i])
    23. continue;
    24. if(s[i]=='b'&&t[i]=='a')
    25. {
    26. int j;
    27. for( j=i-1;j>=0;j--)
    28. {
    29. if(s[j]=='a'||s[j]=='b')
    30. {
    31. if(s[j]=='a')
    32. {
    33. swap(s[j],s[i]);
    34. break;
    35. }
    36. }
    37. else
    38. {
    39. flag=1;
    40. break;
    41. }
    42. }
    43. if(j==-1)
    44. flag=1;
    45. }
    46. else if(s[i]=='c'&&t[i]=='b')
    47. {
    48. int j;
    49. for( j=i-1;j>=0;j--)
    50. {
    51. if(s[j]=='c'||s[j]=='b')
    52. {
    53. if(s[j]=='b')
    54. {
    55. swap(s[j],s[i]);
    56. break;
    57. }
    58. }
    59. else
    60. {
    61. flag=1;
    62. break;
    63. }
    64. }
    65. if(j==-1)
    66. flag=1;
    67. }
    68. else
    69. {
    70. flag=1;
    71. break;
    72. }
    73. }
    74. if(flag)
    75. {
    76. cout<<"NO"<
    77. }
    78. else
    79. {
    80. cout<<"YES"<
    81. }
    82. }
    83. return 0;
    84. }

  • 相关阅读:
    uniapp左滑列表删除
    2.3.C++项目:网络版五子棋对战之实用工具类模块的设计
    【无标题】C语言学习笔记5--循环结构和选择结构
    合格vue开发者应该知道的面试题
    什么是TCP粘包?如何避免TCP粘包?
    UE5 - ArchvizExplorer - 数字孪生城市模板 - 功能修改
    阿斯达年代记三强争霸账号怎么注册 游戏账号注册教程分享
    node 第七天 手写前后端jsonp(一个古老的绕过跨域的方法)
    HashData获得华为鲲鹏Validated认证 信创版图持续壮大
    Rabin加解密算法(python3)
  • 原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126138522