键盘 keyboard.cpp

本文探讨了一种涉及字符串操作的算法问题,包括删除、插入、复制及清空字符串的代价计算,采用动态规划与最短路径算法SPFA,优化解决从特定长度到目标长度的最小操作成本。

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

【一句话题意】有多种操作

1、删除一个字符,代价为1
2、加入一个字符,代价为1
3、复制字符串为原来的k倍,代价为2+2*k
3、删除所有字符,代价为3

现在有初始长度为x,求到长度n最小代价。

【分析】
题目稍作简化,请无视。
显然删除所有字符只有在所有操作之前(反证法即可)。
稍作思考即可发现,dp值的大小,存在极强的后效性。所以,改成最短路求dist[x],spfa玄学复杂度再加常数优化。

【code】

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define rint register int
using namespace std;
const int maxn=1e6+1000;
int dist[maxn<<1];
bool inq[maxn<<1];
struct Queue{
	int a[maxn<<1];
	int l,r;
	bool empty(){return l==r;}
	void push(int x){a[r++]=x;r%=maxn<<1;}
	int front(){return a[l];}
	void pop(){l++;l%=maxn<<1;}
}q;
int main(){
	freopen("keyboard.in","r",stdin);
	freopen("keyboard.out","w",stdout);
//	queue<int>q;
	rint x,n;
	cin>>x>>n;
	memset(dist,0x3f,sizeof(dist));
	dist[0]=3,dist[x]=0;
	inq[0]=1,inq[x]=1;
	q.push(0),q.push(x);
	while(!q.empty()){
		rint x=q.front();q.pop();inq[x]=0;
		if(x){
			for(rint i=2;i*x<=(n<<1);i++){
				if(dist[i*x]>dist[x]+(i<<1)+2){
					dist[i*x]=dist[x]+(i<<1)+2;
					if(!inq[i*x]) inq[i*x]=1,q.push(i*x);
				}
			}
		}
		for(rint i=x-1;i>=0;i--){
			if(dist[i]<=dist[x]+(x-i)) break;
			dist[i]=dist[x]+(x-i);
			if(!inq[i]) inq[i]=1,q.push(i);
		}
		for(rint i=x+1;i<=(n<<1);i++){
			if(dist[i]<=dist[x]+(i-x)) break;
			dist[i]=dist[x]+(i-x);
			if(!inq[i]) inq[i]=1,q.push(i);
		}
	}
	cout<<dist[n]<<endl;
	return 0;
}
``` QT += widgets charts virtualkeyboard #quickwidgets quick TEMPLATE = app CONFIG += c++11 CONFIG += release CONFIG += qml_release#qml_debug TARGET = careray_XCamera INCLUDEPATH += . # The following define makes your compiler emit warnings if you use # any Qt feature that has been marked deprecated (the exact warnings # depend on your compiler). Refer to the documentation for the # deprecated API to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 INCLUDEPATH += Zmq SOURCES += \ Zmq/CommandSocket.cpp \ Zmq/ZmqSocket.cpp \ Zmq/imagesocket.cpp \ appconfig.cpp \ confighandle.cpp \ demo.cpp \ imageprovider.cpp \ informationhandle.cpp \ jpegimage.cpp \ jsonconfig.cpp \ main.cpp \ messagehandle.cpp \ patienthandle.cpp \ radimage.cpp \ tracehandle.cpp \ type.cpp RESOURCES += qml.qrc # Additional import path used to resolve QML modules in Qt Creator's code model # Additional import path used to resolve QML modules just for Qt Quick Designer # Default rules for deployment. #qnx: target.path = /tmp/$${TARGET}/bin #else: unix:!android: target.path = /opt/$${TARGET}/bin #!isEmpty(target.path): INSTALLS += target HEADERS += \ Zmq/CommandSocket.h \ Zmq/ZmqSocket.h \ Zmq/imagesocket.h \ appconfig.h \ confighandle.h \ demo.h \ errorcode.h \ imageprovider.h \ informationhandle.h \ jpegimage.h \ jsonconfig.h \ messagehandle.h \ patienthandle.h \ radimage.h \ tracehandle.h \ type.h INCLUDEPATH += $$PWD/ThirdParty/include/zmq \ $$PWD/ThirdParty/include/dcmtk LIBS += -L$$PWD/ThirdParty/lib/zmq/x64 -llibzmq \ -L$$PWD/ThirdParty/lib/dcmtk \ -lIphlpapi \ -lws2_32 \ -lwsock32 \ -lnetapi32\ -lofstd \ -loflog \ -ldcmdata \ -lzlib_o \ -ldcmimgle \ -ldcmimage \ -lijg8 \ -lijg12 \ -lijg16 \ -ldcmjpeg #LIBS += -llibzmq \ # -ldcmimgle -ldcmimage -ldcmdata -lofstd -ldcmjpeg DEPENDPATH += $$PWD/ThirdParty/lib/zmq/x64 \ $$PWD/ThirdParty/lib/dcmtk```分析这个.pro文件的问题,并输出优化后的结果
最新发布
03-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值