[2019CCPC-江西省赛] D - Wave 暴力/dp

题目链接:D - Wave

题意

给你一个长度为n的字符串,字符串只包含1~c数字,让你找一个子字符串要求子字符串的奇数位为一个数,偶数位为一个数并且奇数位和偶数位不同。

题解

本题有一个关键点c≤100,很明显我们可以通过枚举奇数位和偶数位的数字来得到答案。当奇数位为i,偶数位为j时,我们可以先统计哪些位置的数字为i,哪些位置的数字为j。然后不断二分去扩展长度,如果无法二分,那么就停止扩展。
这是暴力解法,时间复杂度为 O ( c 2 l o g ( n ) ) {O(c^2log(n))} O(c2log(n))

当然这道题也可以用动态规划去求解。
我们可以设 dp[i][j]:当前位为i,上一位为j的长度。
由于奇数位的相同,偶数位的相同。那么上一位的上一位数字就是当前位的数字。
状态转移很容易得出:
d p [ i ] [ j ] = d p [ j ] [ i ] + 1 {dp[i][j]=dp[j][i]+1} dp[i][j]=dp[j][i]+1
我们每输入一个当前位,然后枚举上一位,再通过状态转移去不断更新答案即可。
时间复杂度为 O ( n c ) {O(nc)} O(nc)

代码

暴力

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
//extern "C"{void *__dso_handle=0;}
typedef long long ll;
typedef long double ld;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define lowbit(x) x&-x

const double PI=acos(-1.0);
const double eps=1e-6;
const ll mod=1e9+7;
const int inf=0x3f3f3f3f;
const int maxn=1e5+10;
const int maxm=100+10;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

vector<int> book[110];
int f(int odd,int even)
{
    
    
	int ans=0;
	if(book[odd].size()) ans=1;
	else return 0;
	int tmp=book[odd][0];
	while(1)
	{
    
    
		vector<int>::iterator it;
		it = lower_bound(book[even].begin(), book[even].end(), tmp);
		if(it==book[even].end()) break;
		tmp=(*it);
		++ans;
		it= lower_bound(book[odd].begin(), book[odd].end(), tmp);
		if(it==book[odd].end()) break;
		tmp=(*it);
		++ans;
	}
	return ans;
}

int main()
{
    
    
	int n,c; scanf("%d%d",&n,&c);
	for(int i=1;i<=n;i++)
	{
    
    
		int t; scanf("%d",&t);
		book[t].pb(i);
	}
	int ans=0;
	for(int i=1;i<=c;i++)
		for(int j=1;j<=c;j++)
		{
    
    
			if(i==j) continue ;
			ans=max(ans,f(i,j));
		}
	printf("%d\n",ans);
}

dp

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
//extern "C"{void *__dso_handle=0;}
typedef long long ll;
typedef long double ld;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define lowbit(x) x&-x

const double PI=acos(-1.0);
const double eps=1e-6;
const ll mod=1e9+7;
const int inf=0x3f3f3f3f;
const int maxn=1e5+10;
const int maxm=100+10;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

int dp[105][105];

int main()
{
    
    
	int n,c;
	cin >> n >> c;
	int maxx=0;
	for(int i=1;i<=n;i++)
	{
    
    
		int tmp; cin >> tmp;
		for(int j=1;j<=c;j++)
		{
    
    
			if(j!=tmp) dp[tmp][j]=dp[j][tmp]+1;
			maxx=max(maxx,dp[tmp][j]);
		}
	}
	cout << maxx << endl;
}

猜你喜欢

转载自blog.csdn.net/weixin_44235989/article/details/108198581