1. 程式人生 > >Ugly Problem dfs+字串+模擬

Ugly Problem dfs+字串+模擬

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 (1
s101000
).
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;
}