zoukankan      html  css  js  c++  java
  • cf字符串操作

    传送门

    Hr0d1y has qq queries on a binary string ss of length nn. A binary string is a string containing only characters '0' and '1'.

    A query is described by a pair of integers lili, riri (1li<rin)(1≤li<ri≤n).

    For each query, he has to determine whether there exists a good subsequence in ss that is equal to the substring s[liri]s[li…ri].

    • A substring s[ij]s[i…j] of a string ss is the string formed by characters sisi+1sjsisi+1…sj.
    • String aa is said to be a subsequence of string bb if aa can be obtained from bb by deleting some characters without changing the order of the remaining characters.
    • A subsequence is said to be good if it is not contiguous and has length 2≥2. For example, if ss is "1100110", then the subsequences s1s2s4s1s2s4 ("1100110") and s1s5s7s1s5s7 ("1100110") are good, while s1s2s3s1s2s3 ("1100110") is not good.

    Can you help Hr0d1y answer each query?

    Input

    The first line of the input contains a single integer tt (1t1001≤t≤100) — the number of test cases. The description of each test case is as follows.

    The first line contains two integers nn (2n1002≤n≤100) and qq (1q1001≤q≤100) — the length of the string and the number of queries.

    The second line contains the string ss.

    The ii-th of the next qq lines contains two integers lili and riri (1li<rin1≤li<ri≤n).

    Output

    For each test case, output qq lines. The ii-th line of the output of each test case should contain "YES" if there exists a good subsequence equal to the substring s[li...ri]s[li...ri], and "NO" otherwise.

    You may print each letter in any case (upper or lower).

    Example

    题目大意就是

    现在有一个字符串s,s的长度为n,s[l...r]表示s的一个子串。

    有q次查询,每次查询给出l与r,求s中是否有非连续的子串是与s[l...r]相等的。


    就是找一个非连续的你可以找一个看两段的

    就是先在(1,l-1)找一个使得a[i]==a[l],如果能找到就输出yes

    如果找不到那就在(r+1,n)找一个使得a[i]==a[r],如果能找到就是出yes

    否则就是输出no

    #include<iostream>
    #include<algorithm>
    #include<map>
    #include<queue> 
    using namespace std;
    typedef long long ll;
    const int maxn=1e5+100;
    char a[maxn];
    //只需要判断在(1,l-1)是否有一个和a[l]相同
    //如果不满足上面的那个就判断(r+1,len)是否有一个和a[r]相同 
    int main(){
        int t;
        cin>>t;
        while(t--){
            int n,m;
            scanf("%d%d",&n,&m);
            scanf("%s",a+1);
            int x,y;
            for(int i=1;i<=m;i++){
                scanf("%d%d",&x,&y);
                int flag=0; 
                for(int i=1;i<x;i++){
                    if(a[i]==a[x]){
                        flag=1;
                        break;
                    }
                }
                for(int i=y+1;i<=n;i++){
                    if(a[i]==a[y]){
                        flag=1;
                        break;
                    }
                }
                if(flag){
                    printf("YES
    ");
                } 
                else{
                    printf("NO
    ");
                }
            }    
            
        } 
    } 

    二:

    传送门

    Ashish has two strings aa and bb, each of length nn, and an integer kk. The strings only contain lowercase English letters.

    He wants to convert string aa into string bb by performing some (possibly zero) operations on aa.

    In one move, he can either

    • choose an index ii (1in1) and swap aiai and ai+1ai+1, choose an index ii (1ink+1) and if ai,ai+1,,ai+k1ai,ai+1,…,ai+k−1 are all equal to some character cc (cc≠ 'z'), replace each one with the next character (c+1), that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on.

    Note that he can perform any number of operations, and the operations can only be performed on string aa.

    Help Ashish determine if it is possible to convert string aa into bb after performing some (possibly zero) operations on it.

    Input

    The first line contains a single integer tt (1t1051≤t≤105) — the number of test cases. The description of each test case is as follows.

    The first line of each test case contains two integers nn (2n1062≤n≤106) and kk (1kn1≤k≤n).

    The second line of each test case contains the string aa of length nn consisting of lowercase English letters.

    The third line of each test case contains the string bb of length nn consisting of lowercase English letters.

    It is guaranteed that the sum of values nn among all test cases does not exceed 106106.

    Output

    For each test case, print "Yes" if Ashish can convert aa into bb after some moves, else print "No".

    You may print the letters of the answer in any case (upper or lower).

    Example
     
    题目大意:
    就是给你两个字符串,对于字符串a,
    有两种操作就是
    1.a[i]和a[i+1]交换
    2.就是可以将连续的K个字符的数值加1,如a->b

    1.对于第一个操作的运用:看到可以交换任意两个相邻字符位置,
     其实等同于可以给所有字符重新排列,
        如此我们需要的只是把a的各个字符相应的数量转变为b的各个字符数量即可。

    2.对于第二操作的应用:

    1.如果mp[a[i]]<mp[b[i]]肯定不行的,

    2.如果mp[a[i]]-mp[b[i]]%k!=0这些字符一定不能全部转变成其他相同的字符,这样的最终结果是最终a中i字符的个数一定大于b中i字符的个数,不符合情况

    3.每次要把多余的字符mp[a[i]]-mp[b[i]]全部变成别的字符,不妨变成下一个字符即i + 1 

    #include<iostream>
    #include<algorithm>
    #include<map>
    using namespace std;
    string a,b;
    map<char,int>mp;
    map<char,int>mp1;
    bool cmp(char a,char b){
        return a<b;
    }
    int main(){
        int t;
        cin>>t;
        while(t--){
            mp.clear();
            mp1.clear();
            int n,m;
            cin>>n>>m;
            cin>>a>>b;
            int len=a.size();
            for(int i=0;i<len;i++){
                mp[a[i]-'a']++;
                mp1[b[i]-'a']++;
            }
            int flag=1;
            for(int i=0;i<26;i++){
                if(mp[i]<mp1[i]||(mp[i]-mp1[i])%m){
                    flag=0;
                    break;
                }
                mp[i+1]+=(mp[i]-mp1[i]);
            }
            if(flag==0){
                printf("No
    ");
            } 
            else{
                printf("Yes
    ");
            }
        }
        return 0; 
    }
  • 相关阅读:
    Egg.js 介绍以及环境搭建
    Redis在Nodejs中的使用
    Android8以上 显示通知栏简单实现
    Android 跳转到系统通知管理页面
    Android APP打开另一个APP的几种实现总结
    Python 库大全 --收集
    python读取Excel数据保存到mongoDB中
    python读取mongoDb数据库保存到Excel中
    Python中的Matplotlib绘图
    数据分析案例:统计电影分类的情况
  • 原文地址:https://www.cnblogs.com/lipu123/p/14056669.html
Copyright © 2011-2022 走看看