lightoj 1396 - Palindromic Numbers (III)

    PDF (English) Statistics Forum
Time Limit: 1 second(s) Memory Limit: 32 MB

Vinci is a little boy and is very creative. One day his teacher asked him to write all the Palindromic numbers from 1 to 1000. He became very frustrated because there is nothing creative in the task. Observing his expression, the teacher replied, "All right then, you want hard stuff, you got it." Then he asks Vinci to write a palindromic number which is greater than the given number. A number is called palindromic when its digits are same from both sides. For example: 1223221, 121, 232 are palindromic numbers but 122, 211, 332 are not. As there can be multiple solutions, Vinci has to find the number which is as small as possible.

Input

Input starts with an integer T (≤ 30), denoting the number of test cases.

Each case starts with a line containing a positive integer. This integer can be huge and can contain up to 105 digits.

Output

For each case, print the case number and the minimum possible palindromic number which is greater than the given number.

Sample Input

Output for Sample Input

5

121

1

1332331

11

1121

Case 1: 131

Case 2: 2

Case 3: 1333331

Case 4: 22

Case 5: 1221

Note

Dataset is huge, use faster I/O methods.

题意:构造回文数字。

对于给定的数字,输出大于这个数的最小回文数字,len <= 100000;

显然这个的第一个想法就是从中间往两边找对应位置上不相同的第一个位置进行相应的修改+1。

和可能本身这个数就是回文的,这个时候就要改中间的数字了+1。问题是这个数字为9怎么办???不是很好进位。

现在就找第一个不为9的位置进行改变,可是万一都是为9的不就完蛋了吗???这里就特判一下全部为9的情况。

再根据奇偶进行不同的出来。

统一分析不全为9的情况。

假定从中间往两边找到了某个对称位置上的数不相同s[x] != s[y];

1.s[x] > s[y],这样的话就s[y] = s[x],然后y后面的全部变成对称位置上的数。

2.s[x] < s[y],这样的话就不能想上面那样来了,由于只能往大的变,且[x,y]区间的数必然是要变的。如果如果直接s[x] = s[y],这样有可能变大了,不是满足条件的最小回文数字。既然都要变,就可以直接从最中间的数字开始考虑怎么变,往两边找第一个不为9的+1,对称位置中间的数清0,y后面的变成对称位置上的数字。

3.如果本身回文的话,同第二种情况一样处理。

/*****************************************
Author      :Crazy_AC(JamesQi)
Time        :2015
File Name   :
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <climits>
using namespace std;
#define MEM(x,y) memset(x, y,sizeof x)
#define pk push_back
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
const double eps = 1e-10;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
char s[100010];
int t, icase = 0;
void solve1(){
	int len = strlen(s);
	int mid = len / 2;
	bool flag = false;
	int pos;
	for (int i = mid + 1;i < len;++i){
		if (s[i] == s[len - i - 1]) continue;
		if (s[i] < s[len - i - 1]){
			flag = true;
			pos = i;
			break;
		}
		break;
	}
	if (flag){
		s[pos] = s[len - pos - 1];
		for (int i = pos + 1;i < len;++i)
			s[i] = s[len - i - 1];
		puts(s);
	}else{
		if (s[mid] != '9'){
			s[mid]++;
			for (int i = mid + 1;i < len;++i)
				s[i] = s[len - i - 1];
			puts(s);
		}else{
			for (int i = mid - 1;i >= 0;--i){
				if (s[i] != '9'){
					s[i]++;
					for (int j = i + 1;j <= mid;++j)
						s[j] = '0';
					break;
				}
			}
			for (int i = mid + 1;i < len;++i)
				s[i] = s[len - i - 1];
			puts(s);
		}
	}
}
void solve2(){
	int len = strlen(s);
	int mid = len / 2;
	int pos;
	bool flag = false;
	for (int i = mid;i < len;++i){
		if (s[i] == s[len - i - 1]) continue;
		if (s[i] < s[len - i - 1]){
			pos = i;
			flag = true;
			break;
		}
		break;
	}
	if (flag){
		s[pos] = s[len - pos - 1];
		for (int i = pos + 1;i < len;++i)
			s[i] = s[len - i - 1];
		puts(s);
	}else{
		if (s[mid - 1] != '9'){
			s[mid - 1]++;
			for (int i = mid;i < len;++i)
				s[i] = s[len - i - 1];
			puts(s);
		}else{
			for (int i = mid - 2;i >= 0;--i){
				if (s[i] != '9'){
					s[i]++;
					for (int j = i + 1;j < mid;++j)
						s[j] = '0';
					break;
				}
			}
			for (int i = mid;i < len;++i)
				s[i] = s[len - i - 1];
			puts(s);
		}
	}
}
int main()
{	
	// freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	scanf("%d",&t);
	while(t--){
		scanf("%s",s);
		bool flag = true;
		int len = strlen(s);
		for (int i = 0;i < len;++i){
			if (s[i] != '9'){
				flag = false;
				break;
			}
		}
		printf("Case %d: ", ++icase);
		if (flag){
			putchar('1');
			for (int i = 0;i < len - 1;++i){
				putchar('0');
			}
			putchar('1');
			puts("");
		}
		else{
			len = strlen(s);
			if (len & 1){
				solve1();
			}else solve2();
		}
	}
	return 0;
}



你可能感兴趣的:(贪心,lightoj)