codeforces - 1203A Circle of Students(思维)
思路很简单,用一个字符串保存原数组,对数组排序之后转成首尾相连的字符串,一个正序一个倒序再判断第一个字符串是不是他们中的子串就行了,这里顺便也练习了一下string的用法
#include<cstdio>
#include<stack>
#include<queue>
#include<cmath>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#define TP 233333333333333
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<char, int> P2;
const int maxn = 1000005;
int arr[maxn];
int main(void) {
int n;
cin >> n;
while(n--) {
int m;
cin >> m;
string s1, s2, rs;
for (int i = 0; i<m; i++) {
cin >> arr[i];
s1 += (arr[i] + '0');
}
sort(arr, arr+m);
for (int i = 0; i<m; i++)
s2 += (arr[i] + '0');
s2 = s2 + s2;
rs = s2;
reverse(rs.begin(), rs.end());
if (s2.find(s1) != string::npos || rs.find(s1) != string::npos)
cout << "YES\n";
else
cout << "NO\n";
}
return 0;
}