B. Yet Another Palindrome Problem


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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章