• awoo‘s Favorite Problem(思维)


    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
    NOs

    思路:

    1,透过操作要求,找到一些相对不变的规则

    2,读假题了,“ab”是个连续的字串,因此a只能相对后移,c只能相对的前移

    3,代码是通过验证通过的,需要符合2,以及去掉b后的字符串要一致

    代码:

    1. #include
    2. using namespace std;
    3. #define int long long
    4. #pragma GCC optimize(2)
    5. #pragma GCC optimize(3,"Ofast","inline")//
    6. const int maxj=3e5+100,mod=998244353;
    7. void solve(){//构造验证,a的位置只会后移,c的位置只会前移,a,c的个数各自相同
    8. int n;cin>>n;
    9. string s,t;
    10. cin>>s>>t;
    11. int cnt1=0,cnt2=0;
    12. int ct1=0,ct2=0;
    13. vector<int>a1,a2,c1,c2;
    14. string ss1="",ss2="";
    15. for(int i=0;i
    16. if(s[i]!='b') ss1+=s[i];
    17. if(t[i]!='b') ss2+=t[i];
    18. if(s[i]=='a') a1.emplace_back(i);
    19. if(s[i]=='c') c1.emplace_back(i);
    20. if(t[i]=='a') a2.emplace_back(i);
    21. if(t[i]=='c') c2.emplace_back(i);
    22. }
    23. if(ss1!=ss2){//a,c相对位置不变,abc,cba这个不做考虑
    24. cout<<"NO"<<'\n';
    25. return ;
    26. }
    27. bool k=0;
    28. for(int i=0;isize();++i){
    29. if(a1[i]>a2[i]){
    30. cout<<"NO"<<'\n';
    31. return ;
    32. }
    33. }
    34. for(int i=0;isize();++i){
    35. if(c1[i]
    36. cout<<"NO"<<'\n';
    37. return ;
    38. }
    39. }
    40. cout<<"YES"<<'\n';
    41. }
    42. int32_t main(){
    43. ios::sync_with_stdio(0);
    44. cin.tie(0);
    45. cout.tie(0);
    46. int t;
    47. cin>>t;
    48. // t=1;
    49. while(t--)solve();
    50. return 0;
    51. }

  • 相关阅读:
    基于PaddlePaddle的工业表计数环境搭建
    HMM隐马尔可夫模型用于序列标注
    高性能网络编程 - 关于单台服务器并发TCP连接数理论值的讨论
    JavaSE => 抽象类和接口
    【typescript - tsc 编译后路径问题/路径别名问题】
    TDSQL-C 真·秒级启停:连接断了,又没断
    hdfs分布式文件系统 默认数据存放路径、及相关配置属性详细解析
    算法-1.两数之和
    五月最近一次面试,被阿里P8测开虐惨了...
    【JS】复习和学习几个好用的js小知识
  • 原文地址:https://blog.csdn.net/m0_63054077/article/details/125708352