AcWing 4997. 更小的数

文章介绍了三种方法解决编程问题,即暴力搜索的优化版本,通过调整查找策略避免数组越界和重复计算;以及使用区间动态规划(DP)以空间换取时间。作者通过实例展示了如何提高代码效率以通过面试题或编程挑战。
摘要由CSDN通过智能技术生成

思路1暴力

找到前面大于后面就反转,相等就向内部找

#include<bits/stdc++.h>
using namespace std;

const int N = 5005;
int a[N];

int main(){
	string n;
	cin >> n;
	int s = n.size(); 
	int cnt = 0;
	for(int i = 0; i < s; i ++ ) a[i] = n[i] - '0';
	for(int i = 0; i < s - 1; i ++ ) {
		for(int j = i + 1; j < s; j ++ ) {
			int l = i, r = j;
			while(a[r] == a[l] && r > l ) {
				r --, l ++;
			}
			if(a[l] > a[r] && r > l ) {
				cnt ++ ;
			}
		}
	}
	cout << cnt;
	return 0;
}

注意l--,r++有可能跳过l=r直接l>r所以不能用l!=r判断

但是三重循环TLE了~~

思路2优化之后的暴力

从内向外找

注意:数组越界、双指针交错,while循环的break

#include<bits/stdc++.h>
using namespace std;

const int N = 5005;
int a[N];

int main(){
	string n;
	cin >> n;
	int s = n.size(); 
	int cnt = 0;
	for(int i = 0; i < s; i ++ ) a[i] = n[i] - '0';
	for(int i = 0; i < s - 1; i ++ ) {
		for(int j = i + 1; j < s; j ++ ) {
			int l = i, r = j;
            if(a[r] < a[l] && r > l) {
                cnt ++;
                l --, r ++;
                while(a[r] == a[l] && l >= 0 && r < s){
                    cnt ++;
                    l --, r ++;
                }
            }
		}
	}
	cout << cnt;
	return 0;
}

可见有时候调整一下暴力的思路就可以AC

思路三区间DP

用二维数组存储了比较过的结果,用空间换时间

#include<bits/stdc++.h>
using namespace std;
const int N=5e3+10;
int n,dp[N][N],res;

//用dp标记这个序列是否符合条件,用res记录

int main(){
    string s;
    cin>>s;
    n=s.size();
//第一遍遍历长度,第二遍遍历起点
    for(int len=2;len<=n;++len)
    {
        for(int l=0;l+len-1<n;++l){
            int r=l+len-1;
            if(s[l]>s[r])dp[l][r]=1;
            else if(s[l]==s[r])dp[l][r]=dp[l+1][r-1];
            res+=(dp[l][r]==1);
        }
    }
    cout<<res<<endl;
    return 0;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值