[ACM] zoj 3816 generalized palindromic number (DFS, brute force enumeration)

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
Author: Lin, Xi
Source: The 2014 ACM-ICPC Asia Mudanjiang regional first round

Solution:

For a given number of N, calculate the maximum number of "replies" less than n. The number of "replies" requires that the same continuous number can be compressed into a number, such as 1233221, which can be compressed to 12321, which is the number of replies.

The idea is to fill in a number for brute force enumeration. First, fill in the first number on the left in the enumeration field from large to small, and then count the number on the right in the enumeration column as it is. Pay attention to pruning, if the current number on the left meets the meaning of the question, you do not need to enumerate the current number any more, because the maximum value is required. The specific idea is in the code comment.

Reference: http://blog.csdn.net/u011345136/article/details/39122741

Code:

# Include <iostream> # include <stdio. h> # include <string. h> using namespace STD; # define ll long longconst int maxn = 20; int left [maxn], right [maxn]; // The Left and Right Parts of the number of replies, such as 1511, 1 1 string STR; // input string int Len; // length ll N; // string represents the number ll Lmax (ll a, LL B) {return A> B? A: B;} ll getnum (int l, int R) // obtain the current number of input values {ll ans = 0; For (INT I = 1; I <= L; I ++) ans = ans * 10 + left [I]; for (INT I = r; I> = 1; I --) // pay attention to the storage order, the last digit of the original number is the first ans = ans * 10 + right [I]; return ans;} ll DFS (INT L, int R, int cur) stored in the left array) // The number of digits on the left L, the number of digits on the Right R, whether the current number is a boundary, such as 2511, the first 2 is the boundary, and the number of input values must be <= 2, other BITs are not boundary and can start from 9 {ll ans =-1; if (L + R-1 = Len) // because l in the DFS parameter, in DFS, the left-side l-bit is assigned, and the recursive parameter is L + 1. Therefore, when L-1 + R) = Len, it is the recursive exit {ans = getnum (L-1, R ); if (ANS> = N) Return-1; // Don't forget, it cannot be greater than or equal to the given number t return ans;} int now = cur? (STR [L-1]-'0'): 9; // assign a value to the L-digit on the left to determine whether the value is a boundary for (INT I = now; I> = 0; I --) // enumerate the L-th digit {left [l] = I; // if the current L is the first or not the first digit, but the current digit is different from the previous one, and the l cannot be the first and the I is 0, and the number is not completed if (L = 1 | | (L> 1 & left [l]! = Left [L-1]) & (! (L = 1 & I = 0) & L + R! = Len) {for (int K = 1; L + R + k <= Len; k ++) // enumerate the number of bits in the right half with the same L-Th {right [R + k] = I; ans = Lmax (ANS, DFS (L + 1, R + K, cur & now = I) ;}} else {ans = Lmax (ANS, DFS (L + 1, R, cur & now = I ));} if (ANS> 0) // if the current ans match the meaning of the question, you do not need to search down. For example, if 455, 1st, 454, and 4 match the meaning of the question, therefore, the first part does not need to enumerate 3 2 1 0. Return ans;} return ans;} int main () {int t; CIN> T; while (t --) {CIN> STR; n = 0; Len = Str. length (); For (INT I = 0; I <Len; I ++) n = N * 10 + STR [I]-'0 '; ll ans = DFS (1, 0, 1); cout <ans <Endl;} return 0 ;}


[ACM] zoj 3816 generalized palindromic number (DFS, brute force enumeration)

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.