T1 瑞神的序列
Examples
完整代码
#include<iostream>
using namespace std;
int n, last, now;
int num = 1;
int main()
{
cin >> n;
cin >> last;
n--;
while (n--)
{
cin >> now;
if (now != last)
num++;
last = now;
}
cout << num << endl;
return 0;
}
T2 消消乐大师——Q老师
Examples
题目分析
- 检查每个位置,看这一个位置及相邻的四个位置是否能消除
- 消除时将对应位置的mark置为一
完整代码
#include<iostream>
#include<queue>
using namespace std;
int mark[40][40], qipan[40][40];
int n, m;
const int dx[6] = { 0,0,1,-1 };
const int dy[6] = { 1,-1,0,0 };
int main()
{
cin >> n >> m;
for (int i = n; i >= 1; i--)
for (int j = 1; j <= m; j++)
cin >> qipan[i][j];
for (int i = n; i >= 1; i--)
{
for (int j = 1; j <= m; j++)
{
int sum = 0;
for (int k = 0; k < 2; k++)
{
int new_x = i + dy[k];
int new_y = j + dx[k];
if (new_x >= 1 && new_y >= 1 && new_x <= n && new_y <= m && qipan[new_x][new_y] == qipan[i][j])
sum++;
}
if (sum == 2)
{
mark[i][j] = 1;
for (int k = 0; k < 2; k++)
{
int new_x = i + dy[k];
int new_y = j + dx[k];
mark[new_x][new_y] = 1;
}
}
sum = 0;
for (int k = 2; k < 4; k++)
{
int new_x = i + dy[k];
int new_y = j + dx[k];
if (new_x >= 1 && new_y >= 1 && new_x <= n && new_y <= m && qipan[new_x][new_y] == qipan[i][j])
sum++;
}
if (sum == 2)
{
mark[i][j] = 1;
for (int k = 2; k < 4; k++)
{
int new_x = i + dy[k];
int new_y = j + dx[k];
mark[new_x][new_y] = 1;
}
}
}
}
for (int i = n; i >= 1; i--)
{
if (!mark[i][1])
cout << qipan[i][1];
else
cout << 0;
for (int j = 2; j <= m; j++)
{
if (!mark[i][j])
cout<<' ' << qipan[i][j] ;
else
cout<< ' ' << 0;
}
if(i!=1)
cout << endl;
}
return 0;
}
T4 咕咕东学英语
Examples
题目分析
- 首先将A,B序列进行转化,将相邻的A、B进行合并,得到ans数组
- 然后开始计算delicious字串个数
- 声明一个st数组,采用dp的思想,分情况进行讨论
从前往后
st[i]表示1-i,delecious字串个数
//st[i] 包含三部分 st[i-1] ,st[i-1]与ans[i]组成的,ans[i]本身组成的
for (int i = 1; i <= tot; i++)
{
ans[i] += st[i] * (st[i] - 1) / 2;
ans[i] += ans[i - 1];
ans[i] += (st[i] - 1) * (st[i - 1] - 1);
for (int j = i - 2; j >= 0; j--)
ans[i] += st[i] * st[j];
}
完整代码
#include<iostream>
#include<string>
using namespace std;
const int maxn = 3e5 + 10;
string s;
int n;
long long st[maxn], ans[maxn];
int main()
{
cin >> n;
cin >> s;
int tot = 0;
int last = s.at(0), nxt, num = 0;
for (int i = 0; i < s.size(); i++)
{
nxt = s.at(i);
if (nxt == last)
st[tot]++;
else
st[++tot]++;
last = nxt;
}
ans[0] = st[0] * (st[0] - 1) / 2;
for (int i = 1; i <= tot; i++)
{
ans[i] += st[i] * (st[i] - 1) / 2;
ans[i] += ans[i - 1];
ans[i] += (st[i] - 1) * (st[i - 1] - 1);
for (int j = i - 2; j >= 0; j--)
ans[i] += st[i] * st[j];
}
cout << ans[tot] << endl;
return 0;
}
note
有时候不涉及什么已学过思想,,