蓝桥杯2027,-蓝桥杯2027年第十五届省赛真题题解(部分) - string str[N]

📅 2026-12-01 👤 拼多多助力营销技巧 📂 赚钱项目 👁️ 315 次阅读

砍柴:蓝桥杯2024年第十五届省赛真题-砍柴 - C语言网 (dotcpp.com)

思路:可以用记忆化搜索sg函数做,设当前数字为x,枚举所有可能用到的质数,假设用到了质数p,那么x会变成x-p,如果x-p中有一个的状态是必败则当前数x的状态为必胜(减去这个质数就能给对面一个必败局面,所以当前局面是必胜态),如果将所有可能用到的质数都枚举完了也无法获胜则当前为必败态。一些细节写在注释上

#include

using namespace std;

#define int long long

const int N = 1e6 + 10;

bool is[N];

vectorp;

int f[N];

void init() // 筛质数

{

for (int i = 2; i < N; i++)

{

f[i] = -1;

if (is[i])

continue;

p.push_back(i);

for (int j = i + i; j < N; j += i)

{

is[j] = 1;

}

}

f[0] = f[1] = 1;

}

// f[x]=-1表示没有更新过的状态 f[x]=1表示先手必败,f[x]=2表示先手必胜

int sg(int x)

{

if (f[x] != -1) 拼多多帮助力入口

return f[x];

for (int i = 0; i < p.size(); i++) // 把所有当前状态可能用的质数跑一遍

{

if (p[i] > x)

break;

if (sg(x - p[i]) == 1) // 用了这个质数后数字会变成x-p[i],如果这个数字的状态是必败,那x这个状态只要用p[i]这个数字就是必胜

return f[x] = 2;

}

return f[x] = 1;

}

void solve()

{

int x;

cin >> x;

cout << sg(x) - 1 << endl;

}

signed main()

{

ios::sync_with_stdio(false);

cin.tie(0);

cout.tie(0);

int t = 1;

cin >> t;

init();

while (t--)

solve();

}

吊坠:蓝桥杯2024年第十五届省赛真题-吊坠 - C语言网 (dotcpp.com)

思路:对于任意,在i和j之间建边,由于字符串i和j是一个环形,也就是起点不确定,那么边权取i和j所有可能的状态中最长公共子串的长度的最大值。这个过程可以用dp实现。建完边后用kruskal算法求出最大生成树算出边权和就是答案。

#include

using namespace std;

#define int long long

const int N = 1e3 + 10;

int n, m;

string str[N];

int op(string s1, string s2) // 求s1和s2的最大边权

{

s1 = " " + s1;

s2 = " " + s2;

vector> dp(s1.size() + 10, vector(s1.size() + 10, 0));int res = 0;

for (int i = 1; i < s1.size(); i++)

{

for (int j = 1; j < s2.size(); j++)

{

if (s1[i] == s2[j])

dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);

res = max(res, dp[i][j]);

// if (s1[i] == s2[j])

// dp[i][j] = dp[i - 1][j - 1] + 1;

// else

// dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);

}

}

return res;

}

struct node

{

int u, v, w;

} nd[1000006];

bool cmp(node p1, node p2)

{

return p1.w > p2.w;

}

int cnt;

int f[N];

int fd(int x)

{

if (f[x] == x)

return f[x];

return f[x] = fd(f[x]);

}

void kruskar()

{

sort(nd, nd + cnt, cmp);

for (int i = 1; i < N; i++)

f[i] = i;

int ans = 0;

for (int i = 0; i < cnt; i++)

{

int u = nd[i].u, v = nd[i].v, w = nd[i].w;

if (fd(u) != fd(v))

{

ans += w;

f[fd(u)] = fd(v);

}

}

cout << ans << endl;

}

void solve()

{

cin >> n >> m;

for (int i = 1; i <= n; i++)

{

cin >> str[i];

str[i] += str[i];

}

for (int i = 1; i <= n; i++)

{

for (int j = i + 1; j <= n; j++)

{

int w = op(str[i], str[j]); // 最大边权建边

if (w > m)

w = m;

nd[cnt++] = {i, j, w};

}

}

// for (int i = 0; i < cnt; i++)

// {

// cout << nd[i].u << " " << nd[i].v << ' ' << nd[i].w << endl;

// }

kruskar();

}

signed main()

{

ios::sync_with_stdio(false);

cin.tie(0);

cout.tie(0);

int t = 1;

// cin >> t;

while (t--)

solve();

}

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧