AtCoder Regular Contest 154 C. Roller (thinking questions)

topic

T(T<=5e3) group of samples, each time given a number n(n<=5e3),

and two arrays a, b of length n, you can do the following any number of times:

Operation: Select a subscript i (1<=i<=n), which will be a_{i}replaced by a_{i+1}, which a_{n+1}is considered to bea_{1}

After several operations, whether a and b are exactly the same, you can output Yes, but you cannot output No

source of ideas

Ling tea group

answer

Note that at least one element of the initial position is required to be equal,

So the instinctive idea is to enumerate the initial point, and then assign values ​​forward or backward violently,

When assigning ai, only consider the original value of ai, the original value of ai+1, and the value assigned later by ai+1

However, this method is false, and the game will always be ac26wa34,

Consider the following example, the same number field can actually be cyclically shifted, beyond the original position

6
1 1 1 2 1 3
1 3 2 2 2 2

According to the ability to translate, it is derived into the practice of problem solving:

Treat a and b as rings respectively, and remove duplicates from segments with the same value to obtain new arrays a' and b'

1. The length of b' is n, which means that the length of a' must also be n, and it corresponds to the original elements one by one

2. The length of b' is less than n, indicating that the total number of types of b is less than n, indicating that the final total number of types of a is less than n.

Then a' can perform at least one operation, cover an element, and after there is a space, you can perform a circular shift operation,

For example, 1123->1223->1233->2311

After supporting such a cyclic shift, only b ' is a subsequence under the condition of a' cyclic shift

Because a' supports two operations, one is circular shift, and the other is assignment overwriting, which overwrites unwanted element types

the code

#include<iostream>
#include<vector>
using namespace std;
const int N=5e3+10,mod=998244353;
int T,n,c[N],d[N];
vector<int>a,b;
void calc(int *x,vector<int>&y){
    y.clear();
    int las=-1;
    for(int i=0;i<n;++i){
        cin>>x[i];
        if(x[i]!=las)y.push_back(x[i]);
        las=x[i];
    }
    if(y.size()>1 && y.back()==y[0])y.pop_back();
}
bool solve(){
    int sa=a.size(),sb=b.size();
    if(sb==n)return a==b;
    for(int i=0;i<sa;++i){
        int cur=0;
        for(int j=0;j<sa;++j){
            if(b[cur]==a[(i+j)%sa])cur++;
            if(cur==sb)return 1;
        }
    }
    return 0;
}
int main(){
    cin>>T;
    while(T--){
        cin>>n;
        calc(c,a);calc(d,b);
        cout<<(solve()?"Yes":"No")<<endl;
    }
    return 0;
}
/*
2
4
1 1 2 3
2 3 3 3
6
1 1 1 2 1 3
1 3 2 2 2 2
*/

Guess you like

Origin blog.csdn.net/Code92007/article/details/128761910