Zoj 3816 generalized palindromic number (search based on Symmetry)

Source: Internet
Author: User
Generalized palindromic number Time Limit: 2 seconds memory limit: 65536 KB

A number that will be the same when it is written forwards or backwards is known as a palindromic number. For example, 1234321 is a palindromic number.

We call a numberGeneralized palindromic number, If after merging all the consecutive same digits, the resulting number is a palindromic number. for example, 122111 is a generalized palindromic number. because after merging, 122111 turns into 121 which is a palindromic number.

Now you are given a positive integerN, Please find the largest generalized palindromic number lessN.

Input

There are multiple test cases. The first line of input contains an integerT(About 5000) indicating the number of test cases. For each test case:

There is only one integerN(1 <=N<= 1018 ).

Output

For each test case, output the largest generalized palindromic number lessN.

Sample Input
41212312241122
Sample output
1112112211121

Find the maximum number of input files smaller than N (after merging the same number consecutively, it is the number of input files)
Train of Thought: The game has always been a digital DP, sweaty, and never knows how to write it. I don't know how to change my mind. I think it's silly. As long as the largest one is found, it can be found quickly. To satisfy the retrieval nature, the method is to search based on symmetry. DFS (Le, RI, limit); number of columns on the left, number of columns on the right, and whether the upper bound flag exists. Enumerate the position on the left (from big to small), and then enumerate the length that matches the position on the right, and the DFS goes down. If there is a result when I is placed on the left side, the result is the largest, and you do not need to search any more. The method for processing less than N is to update the answer if> = n.
Code:
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <cmath> # include <string> # include <map> # include <stack> # include <vector> # include <set> # include <queue> # define maxn 401 # define maxn 200005 # define INF 0x3f3f3f # define mod 1000000007 # define EPS 1e-6const double Pi = ACOs (-1.0 ); typedef long ll; using namespace STD; ll n, m, ans; int tot, dig [20], le [20], RI [20]; ll get (INT nu M1, int num2) {int I; ll res = 0; for (I = 1; I <= num1; I ++) {res = res * 10 + le [I];} for (I = num2; I> = 1; I --) {res = res * 10 + Ri [I];} return res;} ll DFS (INT num1, int num2, int limit) {ll T, Res = 0; If (num1 + num2> ToT) {res = get (num1-1, num2); If (RES <m) return res; return 0;} int I, j, Ed; ED = limit? Dig [tot-num1 + 1]: 9; for (I = Ed; I> = 0; I --) {le [num1] = I; if (num1 = 1 | le [num1]! = Le [num1-1]) &! (Num1 = 1 & I = 0) & (num1 + num2 <ToT) {for (j = 1; num1 + num2 + j <= tot; j ++) // The number of matching r values {Ri [num2 + J] = I; t = DFS (num1 + 1, num2 + J, limit & (I = ed); Res = max (Res, T) ;}} else {T = DFS (num1 + 1, num2, limit & (I = ed); Res = max (Res, T);} If (RES> 0) return res;} void solve () {int I, j; Tot = 0; M = N; while (n) {dig [++ tot] = n % 10; N/= 10 ;} ans = DFS (1, 0, 1); printf ("% LLD \ n", ANS);} int main () {int I, j, T; scanf ("% d", & T); While (t --) {scanf ("% LLD", & N); solve () ;}return 0 ;}

Zoj 3816 generalized palindromic number (search based on Symmetry)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.