1159

/*
DP

此题的方程是 DP[i][j],i是左下标,j是右下标
DP[i][j] 表示从i到j的字符串需要加多少个字符才能使其成为回文串

if( in[i]==in[j] )
	DP[i][j]  = DP[i+1][j-1];
else
	DP[i][j] = MIN(DP[i+1][j],DP[i][j-1])+1;

但是此题的内存要求很小,DP[N][N]行不通。从方程中可以看出,i只和i+1有关,所以可以使用滚动数组

起初我使用的mod = 3的滚动。按照长度来进行DP的,这样两层的滚动不够,因为除了当前和减一,还有减二的情况
所以,需要三层

还有一种构造滚动数组的方法是:不使用长度来进行DP,而是使用下标。
对于这样做的好处是可以少一层数组
if in[i]== in[j]
	DP[i%2][j] = DP[(i+1)%2][j-1]; (i+1)%2是上次从i-1处开始的DP结果
else
	DP[i%2][j] = MIN(DP[(i+1)%2][j],DP[i%2][j-1])+1;
*/

// include file
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <ctime>

#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <bitset>

#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <list>
#include <functional>

using namespace std;

// typedef
typedef long long LL;
typedef unsigned long long ULL;

// 
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#define FORi(a,b,c) for(int i=(a);i<(b);i+=c)
#define FORj(a,b,c) for(int j=(a);j<(b);j+=c)
#define FORk(a,b,c) for(int k=(a);k<(b);k+=c)
#define FORp(a,b,c) for(int p=(a);p<(b);p+=c)
#define FORii(a,b,c) for(int ii=(a);ii<(b);ii+=c)
#define FORjj(a,b,c) for(int jj=(a);jj<(b);jj+=c)
#define FORkk(a,b,c) for(int kk=(a);kk<(b);kk+=c)

#define FF(i,a)    for(int i=0;i<(a);i++)
#define FFD(i,a)   for(int i=(a)-1;i>=0;i--)

#define Z(a) (a<<1)
#define Y(a) (a>>1)

const double eps = 1e-6;
const double INFf = 1e100;
const int INFi = 1000000000;
const LL INFll = (LL)1<<62;
const double Pi = acos(-1.0);

template<class T> inline T sqr(T a){return a*a;}
template<class T> inline T TMAX(T x,T y)
{
	if(x>y) return x;
	return y;
}
template<class T> inline T TMIN(T x,T y)
{
	if(x<y) return x;
	return y;
}
template<class T> inline void SWAP(T &x,T &y)
{
	T t = x;
	x = y;
	y = t;
}
template<class T> inline T MMAX(T x,T y,T z)
{
	return TMAX(TMAX(x,y),z);
}
template<class T> inline T MMIN(T x,T y,T z)
{
	return TMIN(TMIN(x,y),z);
}


// code begin
/*
int N;
char in[5010];
int DP[2][5010];
int main()
{
	read;
	write;
	while(scanf("%d",&N)==1)
	{
		scanf("%s",in);
		memset(DP,0,sizeof(DP));
		for(int i=N-2;i>=0;i--)
		{
			for(int j=i+1;j<N;j++)
			{
				if(in[i]==in[j])
				{
					DP[i&1][j] = DP[(i+1)&1][j-1];
				}
				else
				{
					DP[i&1][j] = TMIN(DP[i&1][j-1],DP[(i+1)&1][j])+1;
				}
			}
		}
		printf("%d\n",DP[0][N-1]);
	}
	return 0;
}
*/

int N;
char in[5010];
int DP[3][5010];
int main()
{
	read;
	write;
	while(scanf("%d",&N)==1)
	{
		scanf("%s",in);
		memset(DP,-1,sizeof(DP));
		FORi(0,N,1) 
		{
			DP[0][i] = 0;
			DP[1][i] = 0;
		}
		// 初试时DP状态1
		FORi(2,N+1,1)
		{
			FORj(0,N,1)
			{
				if(j+i-1>=N) break;
				if(in[j]==in[j+i-1])
				{
					DP[i%3][j] = DP[(i-2+3)%3][j+1];
				}
				else
				{
					DP[i%3][j] = TMIN(DP[(i-1+3)%3][j]+1,DP[(i-1+3)%3][j+1]+1);
				}
			}
		}
		printf("%d\n",DP[N%3][0]);
	}
	return 0;
}

转载于:https://www.cnblogs.com/ac2012/archive/2011/04/12/2013984.html

### 题目解析 ZZULI OJ 1159 是关于寻找数组中最大的两个数的问题。该问题属于指针专题,主要考察如何通过编程实现高效的算法来找到给定数据中的最大值和次大值。 #### 输入与输出说明 输入部分分为两行: - **第一行**:一个整数 \( n \),表示后续数组的长度,满足条件 \( 1 < n \leq 1000 \)[^3]。 - **第二行**:由空格分隔的 \( n \) 个整数[^4]。 程序需要输出一行,包含两个整数——分别是数组中的最大值和次大值。 --- ### 解题思路 为了高效解决此问题,可以采用一次遍历的方法,在时间复杂度为 \( O(n) \) 的情况下完成计算: 1. 初始化两个变量 `max1` 和 `max2` 来存储当前的最大值和次大值。初始时可设它们为负无穷大(或小于任何可能输入数值的一个极小值),以便处理正数、负数混合的情况[^3]。 2. 对于每一个读取到的数字 \( num \),依次执行以下操作: - 如果 \( num > max1 \),则更新次大值 \( max2 = max1 \),并将新的最大值赋值为 \( max1 = num \)。 - 否则如果 \( num > max2 \) 并且 \( num \neq max1 \),仅更新次大值 \( max2 = num \)。 3. 完成遍历后,输出最终得到的 `max1` 和 `max2` 值即可。 这种方法的优点在于只需扫描整个列表一遍,因此效率较高,尤其适合较大的输入规模。 --- ### 实现代码 以下是基于上述逻辑编写的 C++ 程序示例: ```cpp #include <iostream> using namespace std; int main() { int n; cin >> n; // 输入数组大小 int num, max1 = INT_MIN, max2 = INT_MIN; for(int i = 0; i < n; ++i){ cin >> num; // 输入第i个整数 if(num > max1){ // 更新最大值 max2 = max1; max1 = num; } else if((num > max2) && (num != max1)){ // 更新次大值 max2 = num; } } cout << max1 << " " << max2; // 输出结果 return 0; } ``` 这段代码实现了对输入数据的一次线性扫描,并利用简单的比较运算完成了目标求解过程。 --- ### 注意事项 当存在重复元素或者所有元素相等情况下的特殊情形需特别注意。例如,若所有的输入均为相同值,则应返回相同的两个值作为最大值和次大值。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值