现在的位置: 首页 > 综合 > 正文

hdu3183

2013年02月14日 ⁄ 综合 ⁄ 共 1519字 ⁄ 字号 评论关闭

A Magic Lamp

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

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
 

Source
 

Recommend
lcy
 

本题要求在原字符串中删除m个字符串,使剩下的组成的数的最小。

我使用贪心做的。在原字符串中找第一个非单增的位置,删除较大数字,m--……直至整个字符串单增。若此时m非零,证明还没有删完m个数字,此时只需删掉最后的m个数字。然后就是输出的时候注意开头的0不输出。

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
using namespace std;

int main()
{
	int m;
	string str;
	while(cin>>str>>m)
	{
		string::iterator iter;
		for(iter=str.begin();iter!=str.end()-1;)
		{
			if(*iter<=*(iter+1))
				iter++;
			else 
			{
				m--;
				str.erase(iter);
				if(iter!=str.begin())iter--;
			}
			if(0==m)break;
		}
		str.erase(str.end()-m,str.end());
		iter=str.begin();
		while(*iter=='0'&&iter!=str.end())
			str.erase(iter);

		if(!str.empty())
			cout<<str<<endl;
		else cout<<"0"<<endl;
	}
	return 0;
}

 

抱歉!评论已关闭.