1. 程式人生 > 實用技巧 >cf字串操作

cf字串操作

傳送門

Hr0d1y hasqqqueries on a binary stringssof lengthnn. A binary string is a string containing only characters '0' and '1'.

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

For each query, he has to determine whether there exists a good subsequence inssthat is equal to the substring

s[liri]s[li…ri].

  • A substrings[ij]s[i…j]of a stringssis the string formed by characterssisi+1sjsisi+1…sj.
  • Stringaais said to be a subsequence of stringbbifaacan be obtained frombbby deleting some characters without changing the order of the remaining characters.
  • A subsequence is said to begoodif it is not contiguous and has length
    2≥2. For example, ifssis "1100110", then the subsequencess1s2s4s1s2s4("1100110") ands1s5s7s1s5s7("1100110") are good, whiles1s2s3s1s2s3("1100110") is not good.

Can you help Hr0d1y answer each query?

Input

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

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

The second line contains the stringss.

Theii-th of the nextqqlines contains two integersliliandriri(1li<rin1≤li<ri≤n).

Output

For each test case, outputqqlines. Theii-th line of the output of each test case should contain "YES" if there exists a good subsequence equal to the substrings[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\n");
            } 
            else{
                printf("NO\n");
            }
        }    
        
    } 
} 

二:

傳送門

Ashish has two stringsaaandbb, each of lengthnn, and an integerkk. The strings only contain lowercase English letters.

He wants to convert stringaainto stringbbby performing some (possibly zero) operations onaa.

In one move, he can either

  • choose an indexii(1in1) and swapaiaiandai+1ai+1, choose an indexii(1ink+1) and ifai,ai+1,,ai+k1ai,ai+1,…,ai+k−1areall equalto some charactercc(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 stringaa.

Help Ashish determine if it is possible to convert stringaaintobbafter performing some (possibly zero) operations on it.

Input

The first line contains a single integertt(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 integersnn(2n1062≤n≤106) andkk(1kn1≤k≤n).

The second line of each test case contains the stringaaof lengthnnconsisting of lowercase English letters.

The third line of each test case contains the stringbbof lengthnnconsisting of lowercase English letters.

It is guaranteed that the sum of valuesnnamong all test cases does not exceed106106.

Output

For each test case, print "Yes" if Ashish can convertaaintobbafter 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\n");
        } 
        else{
            printf("Yes\n");
        }
    }
    return 0; 
}