A
image.png
分析:
就是簽到題orz淹辞,遇到和前一個(gè)不一樣的數(shù),answer就++竭翠;
#include<iostream>
using namespace std;
int main()
{
int k;cin>>k;
int c;cin>>c;
int temp;
int sum=1;
for(int i=1;i<k;i++)
{
temp=c;
cin>>c;
if(c!=temp) sum++;
}
cout<<sum<<endl;
return 0;
}
B
image.png
思路:
分開橫豎去判斷兩輪振坚,最后合在一起,其實(shí)就是個(gè)模擬的過程
#include<iostream>
using namespace std;
int kl[35][35];
int xiao1[35][35];
int xiao2[35][35];
int main()
{
int n,m;cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>kl[i][j];
xiao1[i][j]=1;
xiao2[i][j]=1;
}
}
int left1;int right1;
int left2;int right2;
for(int i=1;i<=n;i++)
{
left1=right1=1;
left2=right2=1;
for(int j=2;j<=m;j++)
{
if(kl[i][j]==kl[i][j-1])
{
right1++;
}
if(kl[i][j]!=kl[i][j-1])
{
// cout<<"he"<<endl;
if((right1-left1+1)>=3)
{
// cout<<i<<"as "<<j<<endl;
for(int p=left1;p<=right1;p++) xiao1[i][p]=0;
}
left1=right1=j;
}
if(kl[j][i]==kl[j-1][i])
{
right2++;
}
if(kl[j][i]!=kl[j-1][i])
{
if((right2-left2+1)>=3)
{
// cout<<i<<" au"<<j<<endl;
for(int p=left2;p<=right2;p++) xiao2[p][i]=0;
}
left2=right2=j;
}
if(j==m)
{
if((right2-left2+1)>=3)
{
//cout<<i<<" au"<<j<<endl;
for(int p=left2;p<=right2;p++) xiao2[p][i]=0;
}
if((right1-left1+1)>=3)
{
// cout<<i<<"as "<<j<<endl;
for(int p=left1;p<=right1;p++) xiao1[i][p]=0;
}
}
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(xiao1[i][j]==0||xiao2[i][j]==0) kl[i][j]=0;
//if(xiao1[i][j]==0) kl[i][j]=-1;
//if(xiao2[i][j]==0) kl[i][j]=-2;
cout<<kl[i][j];
if(j!=m)
{
cout<<" ";
}
}
cout<<endl;
}
return 0;
}
image.png
image.png
這個(gè)題就比較麻煩了斋扰。開始寫的模擬居然爆0 QAQ渡八。
其實(shí)考慮一下,不滿足條件的字段就是AB ABBBB传货。呀狼。。 BA BAAAA损离。。然后全部的子串?dāng)?shù)量是有公式算出來
然后為了減少時(shí)間復(fù)雜度绝编。需要在代碼種一次循環(huán)找到所有的這些子串?dāng)?shù)量僻澎。
代碼如下
#include<iostream>
#include<string>
#include<string.h>
#include<cstring>
using namespace std;
int c[2000000][2];
string x;
int w=0;
bool panduan(int x1,int y1)
{
bool ok=1;
int middle=(x1+y1)/2;
int cs=0;
for(int i=x1;i<=middle;i++)
{
if(x[x1]!=x[y1-cs])
{
ok=0;break;
}
}
if(ok)
{
c[w][0]=x1;c[w][1]=y1;
w++;
}
else return 0;
}
int main()
{
int n;
cin>>n;
cin>>x;
int left=0;int right=1;
while(left<n)
{
if(right==left) right++;
if(panduan(left,right))
{
left++;right=left+1;
}
else
{
right++;
}
if(right>n-1)
{
left++;
right=left+1;
}
}
int sum=0;
int lf,rt;
for(int i=0;i<w;i++)
{
sum++;
lf=c[i][0];rt=c[i][1];
for(int j=i+1;j<w;j++)
{
if(c[j][0]<=rt+1&&c[j][1]>rt)
{
sum++;
rt=c[j][1];
}
else break;
}
}
cout<<sum<<endl;
return 0;
}