題目鏈接:點擊打開鏈接
題目A,B,C;
比賽當時只做出了三道題,下來之後想補D題,但是想了很久都不明白。唉
題目A: 簽到
#include <iostream>
#include <map>
#include <set>
#include <string>
#include<string.h>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
int main(){
string s;
string t="AC";
cin>>s;
if(s.find(t)!=-1) printf("Yes\n");
else printf("No\n");
return 0;
}
題目B:暴力
我的想法是,先考慮兩個數a,b的情況。
如果a是奇數,那麼相鄰的兩個數就是偶數,同理,如果a是偶數,那麼相鄰的兩個數就是奇數。
只有奇數*奇數=奇數。剩下三種情況相乘之後都是偶數。
比如樣例1:2,3
那麼就是 2odd+1even 2even+1odd 相乘之後奇數的個數 odd=2*1=2 even=3*3-odd=7;
如果n=3,那麼先把前兩個算出來,比如 n=3: 2 3 3;前兩個數算完之後就 是 odd=2 even=7 在和後面的3 2even+1odd 計算即可,這樣推下去就行。暴力,記得開long long
#include <iostream>
#include <map>
#include <set>
#include <string>
#include<string.h>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
int c[27][27]={0};
void play_table(void)
{
for(int i=0;i<=26;i++)
for(int j=0;j<=i;j++)
if(!j || i==j)
c[i][j]=1;
else
c[i][j]=c[i-1][j-1]+c[i-1][j];
c[0][0]=0;
return;
}
bool f[15];
int e[15],o[15];
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
int x;
scanf("%d",&x);
if(x&1) f[i]=true;//odd 1
else f[i]=false;// even 0
if(f[i]){
e[i]=2;
o[i]=1;
}else{
e[i]=1;
o[i]=2;
}
}
int ans;
int even=0,odd=0;
/*
cure=e[1]*e[2]+e[1]*o[2]+o[1]*e[2];
curo=(e[1]+o[1])*(e[2]+o[2])-cure;
*/
odd=o[1]*o[2];
even=(o[1]+e[1])*(o[2]+e[2])-odd;
//printf("cure=%d,curo=%d\n",cure,curo);
for(int i=2;i<n;i++){
int tmp=odd;
odd=odd*o[i+1];
even=(tmp+even)*(o[i+1]+e[i+1])-odd;
/*
cure=cure*e[i+1]+curo*e[i+1]+cure*o[i+1];
curo=(cure+curo)*(e[i+1]+o[i+1])-cure;
printf("cure=%d,curo=%d\n",cure,curo);
*/
}
if(n==1){
if(f[1]) printf("0\n");
else printf("1\n");
}else{
printf("%d\n",even);
}
return 0;
}
C題:模擬一下。
首先考慮把所有的X去掉,如果剩下的串是不是迴文串,那麼不論怎麼往字符串裏面增加X都不可能湊成迴文串。
當去掉X之後的串是迴文串之後,我們再處理。
設置兩個指針,head tail 頭指針與尾指針
如果掃到的兩個字符都是X 那麼head+1 tail-1
如果掃到的兩個字符只有一個是X 另外一個不是X 比如 xaxba 下標從0-4 head=0 tail=4 s[head]='X' s[tail]='a' 我們應該在tail 後面加一個X,變成 xaxbax 對吧,那麼我們模擬這個過程,head+1 ,tail不動,ans+1;
不存在掃到兩個不爲‘X’的字符並且字符不等。就這樣模擬就行啦。
#include <iostream>
#include <map>
#include <set>
#include <string>
#include<string.h>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
int main(){
string s,t;
cin>>s;
t=s;
for(int i=s.length()-1;i>=0;i--){
if(s[i]=='x'){
t.erase(t.begin()+i);
}
}
//cout<<t<<endl;
bool p=true;
int len=t.length();
for(int i=0;i<=(len/2);i++){
if(t[i]!=t[len-1-i]){
p=false;
break;
}
}
if(!p) printf("-1\n");
else{
int ans=0;
int head=0,tail=s.length()-1;
for(int i=0;i<s.length();i++){
if(tail<=head) break;
if(s[head]!=s[tail]){
if(s[head]=='x'){
head++;
ans++;
}else if(s[tail]=='x'){
tail--;
ans++;
}
}else if(s[head]==s[tail]){
tail--;
head++;
}
}
printf("%d\n",ans);
}
return 0;
}