B. Yet Another Palindrome Problem

本文探讨了在序列中寻找长度大于等于3的回文子序列的问题,提出了两种解决方案:一种是n方复杂度的简单搜索方法,另一种是更高效的O(n)算法,通过使用vector存储相同元素的位置来优化搜索过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


B. Yet Another Palindrome Problem


标签


简明题意

  • 给一个序列,问是否存在长度>=3的子序列且为回文。

思路

  • 对于每一个a[i],从j=i+2开始找如果有找到某个a[j]=a[i],就yes。这种复杂度n方。而题目的n是5000,所以可以过。
  • 讲讲O(n)的算法。开一个vector<int> g[maxn],然后在每个vector里暴力找。

注意事项


总结


AC代码

#pragma GCC optimize(2)
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring> 
#include<stack>
#include<map>
#include<queue>
#include<cstdio>	
#include<set>
#include<map>
#include<string>
using namespace std;

void solve()
{
	int t;
	cin >> t;
	while (t--)
	{
		vector<int> g[5000 + 10];
		int n;
		cin >> n;
		for (int i = 1; i <= n; i++)
		{
			int t;
			cin >> t;
			g[t].push_back(i);
		}

		bool ok = 0;
		for (int i = 1; i <= 5000; i++)
		{
			int las = -1;
			for (auto& it : g[i])
				if (las == -1) las = it;
				else if (it - las >= 2)
				{
					ok = 1;
					break;
				}
			if (ok)
			{
				cout << "YES" << endl;
				break;
			}
		}
		if (!ok) cout << "NO" << endl;
	}
}
	
int main()
{
	//freopen("Testin.txt", "r", stdin);
	solve();
	return 0;
}

### USACO 2025 February Contest Bronze Division Problems and Solutions #### Problem Overview The United States of America Computing Olympiad (USACO) holds contests four times a year, including the February contest. For the Bronze division, participants face algorithmic challenges designed to test basic programming skills and problem-solving abilities. Since specific details about the exact problems from the USACO 2025 February Contest are not available yet, one can infer based on previous years' patterns what types of questions might appear[^1]. Typically, these include tasks involving simple algorithms like sorting, searching, or handling strings and arrays efficiently. For instance, past contests have featured games between characters such as Bessie playing with palindromes where players take turns removing stones equal to palindrome numbers until no more moves remain[^2]. Another common type involves optimizing certain conditions over circular structures which require careful consideration when iterating through elements due to wrap-around properties[^3]. Given this context, let's consider an example similar to those seen before: #### Example Problem: Palindrome Game Simulation In a game played by two individuals named Bessie and her friend, they start with a pile containing some number of pebbles. Players alternate taking away any positive integer amount of rocks that forms a palindrome sequence during their turn. The player who removes all remaining items wins the match. Given multiple starting configurations represented by `T` different scenarios, determine whether Bessie has winning strategy assuming optimal play from both sides. To solve this kind of challenge programmatically, dynamic programming techniques could prove useful alongside checking for possible valid moves at each step while keeping track of previously encountered states to avoid redundant calculations. ```cpp #include <iostream> using namespace std; bool is_palindrome(int num){ string s = to_string(num); int l=0,h=s.size()-1; while(l<h && s[l]==s[h]) { ++l; --h; } return h<=l; } string winner(int piles[], int size){ bool dp[size+1]; fill(dp,dp+size+1,false); for(int i=1;i<=size;++i){ for(int j=1;j<=i&&j<1000;++j){ // limit check for practical purposes if(is_palindrome(j)){ if(!dp[i-j]){ dp[i]=true;break; } } } } return dp[size]? "B":"E"; } int main(){ int T,pile_size; cin>>T; while(T--){ cin >> pile_size; cout << winner(&pile_size, pile_size)<<endl; } } ``` This code snippet demonstrates how to implement logic determining winners under given constraints using C++ language features effectively.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值