ZOJ3816-Generalized palindromic number (DFS digital search)

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
Question: 1 ~ The largest number in the N-1 (This number is compressed into a text string) idea: using the digital DP idea, enumerate each bit, both the left and right enumeration, from large to small enumeration.
#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;typedef long long LL;const int maxn = 20;int lft[maxn],rgt[maxn];vector<int> digit;LL n,ans;int len;LL getS(int L,int R) {LL ret = 0;for(int i = 0; i < L; i++){ret *= 10;ret += lft[i];}for(int i = R; i >= 1; i--) {ret *= 10;ret += rgt[i];}return ret;}LL dfs(int L,int R,bool done) {    if(L+R == len) {        LL tmp = getS(L,R);        if(tmp <= n-1) return tmp;        else return 0;    }int end = done?digit[L]:9;LL ans = 0;for(int i = end; i >= 0; i--) {lft[L] = i;if(L+R!=len-1&&(L==0||(L>=1&&lft[L]!=lft[L-1]))&&(L!=0||i!=0)){            for(int k = 1; L+R+k <= len-1; k++) {                rgt[k+R] = i;                ans = max(dfs(L+1,R+k,done&&i==end),ans);            }}else{    ans = max(dfs(L+1,R,done&&i==end),ans);}if(ans != 0) return ans;}return 0;}void init() {ans = 0;digit.clear();scanf("%lld",&n);LL x = n;--x;while(x) {digit.push_back(x%10);x /= 10;}len = digit.size();reverse(digit.begin(),digit.end());}void solve() {    ans = dfs(0,0,true);    printf("%lld\n",ans);}int main() {int ncase;cin >> ncase;while(ncase--) {init();solve();}return 0;}


ZOJ3816-Generalized palindromic number (DFS digital search)

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.