10.饿饿 饭饭再升级
错误点:unique前要先sort,导致就算对拍也找了一个小时
代码:
#include<bits/stdc++.h>
using namespace std;
char ch[10001];
int main(){
int n;
cin>>n;
while(n--){
int t;
cin>>t;
for(int i=1;i<=2*t;i++){
if(i<=t)cin>>ch[i];
else ch[i]=ch[i-t];
}
int l=(t/2)+1;
int r=l+t-1;
bool ans=0;
//for(int i=1;i<=t*2;i++)cout<<ch[i];
//cout<<endl;
//cout<<l<<" "<<r<<endl;
for(int wei=l;wei<=r;wei++){
if(ans)break;
int wei_l=wei;
int wei_r=wei;
int L=0;
while(wei_l>=1 && wei_r<=2*t && ch[wei_l]==ch[wei_r]){
wei_l--;wei_r++;L+=2;
}
L--;
if(L>=t)ans=1;
//cout<<wei<<" "<<L<<endl;
}
for(int wei=l;wei<=r;wei++){
if(ans)break;
if(ch[wei]!=ch[wei-1])continue;
int wei_l=wei-1;
int wei_r=wei;
int L=0;
while(wei_l>=1 && wei_r<=2*t && ch[wei_l]==ch[wei_r]){
wei_l--;wei_r++;L+=2;
}
if(L>=t)ans=1;
//cout<<wei<<" "<<L<<endl;
}
if(ans)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
1.
删删
思路:枚举+双指针
#include<bits/stdc++.h>
using namespace std;
int t;
struct cjc{
int id;
char val;
}ch[100010];
int main(){
cin>>t;
while(t--){
char ans='0';
int max_ans=1e9;
int n;
cin>>n;
string str;
cin>>str;
for(int i=1;i<=27;i++){
bool book[100010];
memset(book,0,sizeof(book));
int cnt=0;
int js=0;
char wei = 'a'+i-1;
for(int j=0;j<n;j++){
if(str[j]==wei)js++,book[j]=1;
else ch[++cnt].val=str[j],ch[cnt].id=j;
}
bool is_ans=1;
//cout<<endl;
for(int j=1;j<=cnt;j++){
if(ch[j].val!=ch[cnt-j+1].val){
is_ans=0;
break;
}
}
if(!is_ans)continue;
if(cnt%2==0){
int zhong_1 = ch[(cnt/2)].id;
int zhong_2 = ch[(cnt/2)+1].id;
//if(wei == 'b')cout<<zhong_1<<" "<<zhong_2<<" "<<js<<endl;
js-=(zhong_2-zhong_1-1);
for(int j=1;;j++){
if(zhong_1-j<0 || zhong_2+j>=n)break;
if(book[zhong_1-j]==1 && book[zhong_2+j]==1)js-=2;
}
}
else{
int zhong = ch[(cnt+1/2)].id;
for(int j=1;;j++){
if(zhong-j<0 || zhong+j>=n)break;
if(book[zhong-j]==1 || book[zhong+j]==1)js-=2;
}
}
max_ans=min(max_ans,js);
}
cout<<(max_ans==1e9?-1:max_ans)<<endl;
}
return 0;
}
2.
快快变大
思路:区间dp
#include<bits/stdc++.h>
using namespace std;
int n;
int a[100010];
int dp[1001][1001];
int cheng[1001][1001];
const int P = 1000003;
int main(){
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i],dp[i][i]=0;
for(int i=1;i<=n;i++){
for(int j=i;j<=n;j++){
if(j==i)cheng[i][j]=a[i];
else{
cheng[i][j]=(cheng[i][j-1]*a[j])%P;
}
}
}
for(int k=2;k<=n;k++){
for(int i=1;i+k-1<=n;i++){
for(int j=1;j<=k-1;j++){
int jia = (cheng[i][i+j-1]-cheng[i+j][i+k-1])*(cheng[i][i+j-1]-cheng[i+j][i+k-1]);
int ce = (dp[i][i+j-1]+dp[i+j][i+k-1]+jia)%P;
dp[i][k+i-1]=max(dp[i][k+i-1],ce);
}
}
}
cout<<dp[1][n];
return 0;
}
3.
饿饿 饭饭2
思路:
每个数除2,3,一直除,看剩下的数是否相等
#include<bits/stdc++.h>
using namespace std;
int caozuo(int x){
while(x%2==0){
x/=2;
}
while(x%3==0){
x/=3;
}
return x;
}
int main(){
int t;
cin>>t;
while(t--){
int c;
cin>>c;
int js=-1;
int x;
bool ans=1;
for(int i=1;i<=c;i++){
cin>>x;
if(ans==0)continue;
x=caozuo(x);
if(js<0)js=x;
else{
if(js!=x)ans=0;
}
}
cout<<(ans?"YES":"NO")<<endl;
}
return 0;
}
4.
子串分值和
思路:
一样的计算对区间的贡献,就是比较特殊一点,懂得都懂
代码:
#include<bits/stdc++.h>
using namespace std;
map<char,int>EVA;
string str;
int main(){
cin>>str;
int len = str.length();
long long ans=0;
for(int i=1;i<=26;i++)EVA['a'+i-1]=-1;
for(int i=0;i<len;i++){
ans+=(len-i)*(i-EVA[str[i]]);
EVA[str[i]]=i;
//cout<<ans<<endl;
}
cout<<ans;
return 0;
}
5.
蒟蒻
思路:两个双向的结构体优先队列
代码:
#include<bits/stdc++.h>
using namespace std;
int t;
struct cjc_1{
int id;
int w,t;
bool cun=1;
bool operator<(const cjc_1& a)const
{
return w>a.w;
}
}jr1[1000010];
struct cjc_2{
int id;
int w,t;
bool cun=1;
bool operator<(const cjc_2& a)const
{
return t>a.t;
}
}jr2[1000010];
bool book_w[1000010];
bool book_t[1000010];
int cnt=0;
int main(){
cin>>t;
priority_queue<cjc_1>EVA_1;
priority_queue<cjc_2>EVA_2;
while(t--){
int op;
cin>>op;
if(op==1){
int w,t;
cin>>w>>t;
if(book_w[w] || book_t[t])continue;
book_w[w]=1;
book_t[t]=1;
jr1[++cnt].id=cnt;
jr1[cnt].w=w;
jr1[cnt].t=t;
jr2[cnt].id=cnt;
jr2[cnt].w=w;
jr2[cnt].t=t;
EVA_1.push(jr1[cnt]);
EVA_2.push(jr2[cnt]);
}
else if(op==2){
while(!EVA_1.empty()){
if(jr1[EVA_1.top().id].cun==0){
EVA_1.pop();
}
else break;
}
if(EVA_1.empty())continue;
cjc_1 wei=EVA_1.top();
jr1[wei.id].cun=0;
jr2[wei.id].cun=0;
book_t[wei.t]=0;
book_w[wei.w]=0;
}
else{
while(!EVA_2.empty()){
if(jr2[EVA_2.top().id].cun==0){
EVA_2.pop();
}
else break;
}
if(EVA_2.empty())continue;
cjc_2 wei=EVA_2.top();
jr2[wei.id].cun=0;
jr1[wei.id].cun=0;
book_t[wei.t]=0;
book_w[wei.w]=0;
}
}
int ans=0;
while(!EVA_1.empty()){
cjc_1 wei=EVA_1.top();
if(jr1[wei.id].cun)ans+=wei.w;
EVA_1.pop();
}
cout<<ans;
return 0;
}
6.
思路:胜力可链接
#include<bits/stdc++.h>
using namespace std;
int a[1000010];
bool cmp(int A,int B){
return A>B;
}
int main(){
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++){
cin>>a[i];
}
sort(a+1,a+n+1,cmp);
int i;
for(i=2;i<=n;i++){
if(a[i-1]-a[i]>k)break;
}
cout<<i-1;
return 0;
}
7.
可重排列
dfs
#include<bits/stdc++.h>
using namespace std;
int n;
int a[100];
int res[100];
int res_cnt=0;
int sum=0;
void dfs(int id){
if(id==sum+1){
for(int i=1;i<=res_cnt;i++){
printf("%d ",res[i]);
}
printf("\n");
return ;
}
for(int i=1;i<=n;i++){
if(a[i]){
res[++res_cnt]=i;
a[i]--;
dfs(id+1);
a[i]++;
res_cnt--;
}
}
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
sum+=a[i];
}
dfs(1);
return 0;
}
8.
思路:见题
代码:
#include<bits/stdc++.h>
using namespace std;
int n,m;
char ch[1000010];
int cnt=0;
int main(){
cin>>n>>m;
long long ans=0;
for(int i=1;i<=n;i++){
string str;
int j;
cin>>j>>str;
int len=str.length();
int S=0;
for(int i=len-1;i>=0;i--){
int po = len-1-i;
if(str[i]<='9' && str[i]>='0'){
S+=pow(j,po)*(str[i]-'0');
}
else if(str[i]<='z' && str[i]>='a'){
S+=pow(j,po)*(str[i]-'a'+36);
}
else {
S+=pow(j,po)*(str[i]-'A'+10);
}
}
ans+=S;
}
//cout<<ans<<endl;
while(ans){
int ren=ans%m;
ans/=m;
if(ren<10){
ch[++cnt]='0'+ren;
}
else if(ren<36){
ch[++cnt]='A'+ren-10;
}
else{
ch[++cnt]='a'+ren-36;
}
}
for(int i=cnt;i>=1;i--)cout<<ch[i];
return 0;
}
9.
循环字串
思路:把两个连城一个,在里面找回文串,根据回文串的长度即可判断
代码:
#include<bits/stdc++.h>
using namespace std;
char ch[10001];
int main(){
int n;
cin>>n;
while(n--){
int t;
cin>>t;
for(int i=1;i<=2*t;i++){
if(i<=t)cin>>ch[i];
else ch[i]=ch[i-t];
}
int l=(t/2)+1;
int r=l+t-1;
bool ans=0;
//for(int i=1;i<=t*2;i++)cout<<ch[i];
//cout<<endl;
//cout<<l<<" "<<r<<endl;
for(int wei=l;wei<=r;wei++){
if(ans)break;
int wei_l=wei;
int wei_r=wei;
int L=0;
while(wei_l>=1 && wei_r<=2*t && ch[wei_l]==ch[wei_r]){
wei_l--;wei_r++;L+=2;
}
L--;
if(L>=t)ans=1;
//cout<<wei<<" "<<L<<endl;
}
for(int wei=l;wei<=r;wei++){
if(ans)break;
if(ch[wei]!=ch[wei-1])continue;
int wei_l=wei-1;
int wei_r=wei;
int L=0;
while(wei_l>=1 && wei_r<=2*t && ch[wei_l]==ch[wei_r]){
wei_l--;wei_r++;L+=2;
}
if(L>=t)ans=1;
//cout<<wei<<" "<<L<<endl;
}
if(ans)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}