题意:要求给个回文字符串,可以重组为一个新的回文字符串,可以输出Yes,不可以输出NO

思路,我们可以对代码对半去,假如这个一半里有两个以上的字符,就可以通过调换两个字符的位置来组成一个新的回文字符串

#include<bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include<vector>
typedef long long ll;
using namespace std;
const ll N=1e5+10;
ll n=0,m,s;
ll a[10]={0,13,23,31,41,53,61,71,83,97};

void test() {
string s;
cin>>s;
s=s.substr(0,s.size()/2);
ll f=unique(s.begin(),s.end())-s.begin();
f==1?cout<<"NO"<<endl:cout<<"Yes"<<endl;
}

int main() {
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int t;
std::cin >> t;

while (t--) {
test();
}

return 0;
}

 题意:|a1−a2|+|a2−a3|+⋯+|an−1−an| 称为数组的对比度,让通过尽量减少数组a中的元素来得到另一个数组b,要求数组b中的元素数目最小

思路:首先我们要对数组进行一个去重unique,用来减少a中重复的数目方便计算,然后我们有个简单的思路,就是5,4,2,1,0,这个数组的对比度其实就是5-0,由此可得一串递减或递增的数组,只要取它的两头的最小数和最大数相减就是一串的递减或递增的数组的总对比,所以用代码if(v[i+1]-v[i]>0&&v[i+2]-v[i+1]>0) ans--;,可以去除递增情况下的两端之间无用的元素,由此得到下面的代码

 

#include<bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include<vector>
typedef long long ll;
using namespace std;
const ll N=1e5+10;
ll n=0,m,s;
ll a[10]={0,13,23,31,41,53,61,71,83,97};
 
void test() {
ll n;
cin>>n;
vector<ll>v(n);
for(ll i=0;i<n;i++)
cin>>v[i];
 
n=unique(v.begin(),v.end())-v.begin();
 
ll ans=n;
for(ll i=0;i<n-2;i++)
{
if(v[i+1]-v[i]>0&&v[i+2]-v[i+1]>0)
ans--;
else if(v[i+1]-v[i]<0&&v[i+2]-v[i+1]<0)
ans--;
}
cout<<ans<<endl;
}
 
int main() {
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int t;
std::cin >> t;
 
while (t--) {
test();
}
 
return 0;
}

posted on 2023-09-04 20:06  塔罗-太阳  阅读(7)  评论(0)    收藏  举报