HDU 3183 A Magic Lamp

这是一道ST算法的应用,使用st算法返回下标。

每次取出(i+1,m+j)区间内的最小数  i为前一次取出的最小值下标。 m为题目中要求取出的数字个数(注:是题目中要求的取出的数字,而不是我们取出的最小数)  。j表示当前取第几个数字(1 。。。。n)。

A Magic Lamp

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 642    Accepted Submission(s): 225


Problem Description
Kiki likes traveling. One day she finds a magic lamp, unfortunately the genie in the lamp is not so kind. Kiki must answer a question, and then the genie will realize one of her dreams.
The question is: give you an integer, you are allowed to delete exactly m digits. The left digits will form a new integer. You should make it minimum.
You are not allowed to change the order of the digits. Now can you help Kiki to realize her dream?

Input
There are several test cases.
Each test case will contain an integer you are given (which may at most contains 1000 digits.) and the integer m (if the integer contains n digits, m will not bigger then n). The given integer will not contain leading zero.

Output
For each case, output the minimum result you can get in one line.
If the result contains leading zero, ignore it.

Sample Input
  
  
178543 4 1000001 1 100001 2 12345 2 54321 2

Sample Output
13
1
0
123
321
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<cmath>

using namespace std;

#define MAXS 1010

int num[MAXS];
int dp[MAXS][20];
int n,m;
string x;

void init()   
{
	m=x.length(); 
	for(int i=1;i<=m;i++)
	{
		num[i]=x[i-1]-'0';
		dp[i][0]=i;
	}
	
	int k=floor(log((double)m)/log(2.0));
	int a,b;
	int z; 
	for(int j=1;j<=k;j++)
	{
		for(int i=1;i+(1<<j)-1<=m;i++)
		{
			z=i+(1<<(j-1));
			a=dp[i][j-1];
			b=dp[z][j-1];
			dp[i][j]=(num[a]<=num[b]?a:b);  
		}
	}	
}

int rmq_index(int i,int j)
{
	int z=floor(log((double)(j-i+1))/log(2.0));
	int a,b;
	a=dp[i][z];
	b=dp[j-(1<<z)+1][z];
	return num[a]<=num[b]?a:b;
}

void dps()
{
	int a=m-n;
	int tag=1;
	int flag=false;
	for(int i=1;i<=a;i++)
	{
		int x=rmq_index(tag,n+i);
		tag=x+1;
		if(num[x]!=0)
			flag=true;
		if(flag)
			cout<<num[x];
		else
			{
				if(num[x]==0)
				{
					if(i==a)
						cout<<num[x];
					else
						continue;
				}
				else
					cout<<num[x];
			}
	}
	cout<<endl;
}

int main()
{

	while(cin>>x>>n)
	{
		init();
		if(n==m)
			cout<<"0"<<endl;   //少了这句话一直在wa 
		else
			dps();
	}
	return 0;
} 


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值