HDU5920 - Ugly Problem dfs+字符串+模拟

25 篇文章 0 订阅
25 篇文章 0 订阅

1.题目描述:

Ugly Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 985    Accepted Submission(s): 347
Special Judge


Problem Description
Everyone hates ugly problems.

You are given a positive integer. You must represent that number by sum of palindromic numbers.

A palindromic number is a positive integer such that if you write out that integer as a string in decimal without leading zeros, the string is an palindrome. For example, 1 is a palindromic number and 10 is not.
 

Input
In the first line of input, there is an integer T denoting the number of test cases.

For each test case, there is only one line describing the given integer s ( 1s101000 ).
 

Output
For each test case, output “Case #x:” on the first line where x is the number of that test case starting from 1. Then output the number of palindromic numbers you used, n, on one line. n must be no more than 50. en output n lines, each containing one of your palindromic numbers. Their sum must be exactly s.
 

Sample Input
  
  
2 18 1000000000000
 

Sample Output
  
  
Case #1: 2 9 9 Case #2: 2 999999999999 1
Hint
9 + 9 = 18 999999999999 + 1 = 1000000000000
 

Source
 

Recommend
wange2014
 
2.题意概述:

将一个数字分成不超过50个回文数字,只要不超过50个就行,不一定要最小。

3.解题思路:

因为是50位需要模拟一下大数加减法从。中间开始向两边遍历并逐位赋值给b,dfs找到不对称的地方时取较小的数,每次找到的b都是小于a的较大回文数字。如a = 123345,则第一个回文数字b = 123321; a = 12223 , b = 12221,;a = 32221,b = 12221

4.AC代码:

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <string>
#define N 1500
using namespace std;
int a[N], b[N], c[N];
struct bignum
{
	char s[N];
	friend bignum operator- (bignum q, bignum p)
	{
		bignum ans;
		int la = strlen(q.s);
		int lb = strlen(p.s);
		memset(a, 0, sizeof(a));
		memset(b, 0, sizeof(b));
		memset(c, 0, sizeof(c));
		for (int i = 0; i < la; i++)
			a[i] = q.s[i] - '0';
		for (int i = 0; i < lb; i++)
			b[i] = p.s[i] - '0';
		for (int i = la - 1, j = lb - 1; i >= 0; i--, j--)
		{
			if (j < 0)
			{
				c[i] = a[i];
			}
			if (a[i] >= b[j])
			{
				c[i] = a[i] - b[j];
			}
			else
			{
				a[i] += 10;
				a[i - 1] -= 1;
				c[i] = a[i] - b[j];
			}
		}
		int i = 0;
		while (c[i] == 0) i++;
		memset(ans.s, 0, sizeof(ans.s));
		int j;
		for (j = 0; i < la; i++, j++)
		{
			ans.s[j] = c[i] + '0';
		}
		ans.s[j] = '\0';
		return ans;
	}
	friend bool operator>= (bignum m, bignum n)
	{
		int la = strlen(m.s);
		int lb = strlen(n.s);
		if (la > lb)
			return 1;
		else if (la < lb)
			return 0;
		for (int i = 0; i < la; i++)
			if (m.s[i] > n.s[i])
				return 1;
			else if (m.s[i] < n.s[i])
				return 0;
		return 1;
	}
};
string ans[55];
int num;
bignum getl(bignum n)
{
	bignum x;
	memset(x.s, 0, sizeof(x.s));
	int len = strlen(n.s);
	int i, cnt;
	for (i = 0, cnt = 0; i < len / 2; i++, cnt++)
		x.s[cnt] = n.s[i];
	x.s[cnt] = '\0';
	return x;
}
bignum getr(bignum n)
{
	bignum x;
	memset(x.s, 0, sizeof(x.s));
	int len = strlen(n.s);
	int i, cnt;
	for (i = (len + 1) / 2, cnt = 0; i < len; i++, cnt++)
		x.s[cnt] = n.s[i];
	x.s[cnt] = '\0';
	return x;
}
void dfs(bignum x)
{
	int len = strlen(x.s);
	bool flag = 0;
	for (int i = 0; i < len; i++)
	{
		if (x.s[i] != x.s[len - i - 1])
		{
			flag = 1;
			break;
		}
	}
	if (!flag)
	{
		ans[num++] = (string)x.s + '\0';
		return;
	}
	flag = 0;
	if (x.s[0] == '1')
	{
		for (int i = 1; i < len; i++)
			if (x.s[i] != '0')
			{
				flag = 1;
				break;
			}
		if (!flag)
		{
			bignum tmp;
			strcpy(tmp.s, "1");
			ans[num++] = "1";
			ans[num++] = (string)(x - tmp).s + '\0';;
			return;
		}
	}
	if (len == 1)
	{
		if (x.s[0] != '0')
			ans[num++] = x.s;
		return;
	}
	bignum l = getl(x);
	bignum r = getr(x);
	bignum newl;
	for (int i = len / 2 - 1; i >= 0; i--)
	{
		newl.s[len / 2 - 1 - i] = l.s[i];
	}
	if (r >= newl)
	{
		if (len & 1)
		{
			ans[num++] = (string)l.s + x.s[len / 2] + (string)newl.s + '\0';;
		}
		else
		{
			ans[num++] = (string)l.s + (string)newl.s + '\0';;
		}
		//cout<< (r - newl).s<<endl;
		dfs(r - newl);
	}
	else
	{
		bignum tmp1, tmp;
		strcpy(tmp1.s, "1");
		bignum res = l - tmp1;
		memset(newl.s, 0, sizeof(newl.s));
		for (int i = len / 2 - 1; i >= 0; i--)
		{
			newl.s[len / 2 - 1 - i] = res.s[i];
		}
		if (len & 1)
		{
			ans[num++] = (string)res.s + x.s[len / 2] + (string)newl.s + '\0';;
			for (int i = 0; i < len; i++)
				tmp.s[i] = ans[num - 1][i];
			tmp.s[len] = '\0';
		}
		else
		{
			ans[num++] = (string)res.s + (string)newl.s + '\0';;
			for (int i = 0; i < len; i++)
				tmp.s[i] = ans[num - 1][i];
			tmp.s[len] = '\0';
		}
		bignum tt = x - tmp;
		dfs(tt);
	}
}

int main()
{
	bignum n;
	int t, kase = 1;
	scanf("%d", &t);
	while (t--)
	{
		memset(n.s, 0, sizeof(n.s));
		cin >> n.s;
		for (int i = 0; i < 55; i++)
			ans[i].clear();
		num = 0;
		dfs(n);
		printf("Case #%d:\n", kase++);
		printf("%d\n", num);
		for (int i = 0; i < num; i++)
			cout << ans[i] << endl;
	}
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值