ZOJ, zoj question category
Description
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: calculate the number of input files smaller than N. The same number of input files can be reduced to a number. Train of Thought: dfs (l, r, eq): l represents the length of the left side, r indicates the length of the right side, eq indicates whether it is in the boundary, and then when the right side matches the left side, enumeration k and k indicate that the same continuous number matches the number on the left side.#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>typedef long long ll;using namespace std;char str[50];int len;char leftnum[50], rightnum[50];char ans[50];ll target;ll getans(int l, int r) {ll ans = 0;for (int i = 1; i <= l; i++)ans = ans * 10 + leftnum[i];for (int i = r; i >= 1; i--)ans = ans * 10 + rightnum[i];return ans;}ll dfs(int l, int r, int eq) {ll ans = -1;if (l + r - 1 >= len) {if (l + r - 1 > len)return -1ll;ans = getans(l - 1, r);if (ans < target)return ans;return -1ll;}int m = eq ? str[l] : 9;for (int i = m; i >= 0; i--) {leftnum[l] = i;if ((l == 1 || leftnum[l] != leftnum[l - 1]) && !(l == 1 && i == 0) && !(l+r == len)) {for (int k = 1; k + r + l <= len; k++) {rightnum[r + k] = i;ans = max(ans, dfs(l + 1, r + k, eq && (i == m)));}}else ans = max(ans, dfs(l + 1, r, eq && i == m));if (ans > 0 )return ans;}return ans;}int main() {int t;scanf("%d", &t);while (t--) {scanf("%lld", &target);sprintf(str+1, "%lld", target);len = strlen(str+1);for (int i = 1; i <= len; i++)str[i] -= '0';printf("%lld\n", dfs(1, 0, 1));}return 0;}
Zoj 1151
I spent a long time on AC ......
In fact, tle does not mean that your program has a problem, but it does mean that your program times out during running. That is to say, your program runs slowly, and the result is later than 1 second to determine the timeout.
There are many reasons for timeout. Some are algorithm problems, but your program has no algorithm problems. Therefore, it is only another possibility, that is, the input and output problems.
The c ++ standard input and output cin cout and the string type are all c ++ objects or classes. You must create an object when using it, it is slow to use. It does not affect small-scale acm problems, but when the input is very large, the use of cin cout is much slower than that of scanf and printf.
String is also slower than vector <char>, while vector <char> is slower than char...
For example, I used 0 ms for scanf and printf, and 700 ms for cin and cout (not changed anywhere ......
I used your program template to change it to char [] and changed the input and output to the AC after functions such as gets, printf, and scanf.
I hope that ACM will develop a good habit. Try to use C functions for input and output. Of course, the standard functions of c ++ are not useless, and some functions in stl will soon be used. For example, sort.
Zoj 1151
I spent a long time on AC ......
In fact, tle does not mean that your program has a problem, but it does mean that your program times out during running. That is to say, your program runs slowly, and the result is later than 1 second to determine the timeout.
There are many reasons for timeout. Some are algorithm problems, but your program has no algorithm problems. Therefore, it is only another possibility, that is, the input and output problems.
The c ++ standard input and output cin cout and the string type are all c ++ objects or classes. You must create an object when using it, it is slow to use. It does not affect small-scale acm problems, but when the input is very large, the use of cin cout is much slower than that of scanf and printf.
String is also slower than vector <char>, while vector <char> is slower than char...
For example, I used 0 ms for scanf and printf, and 700 ms for cin and cout (not changed anywhere ......
I used your program template to change it to char [] and changed the input and output to the AC after functions such as gets, printf, and scanf.
I hope that ACM will develop a good habit. Try to use C functions for input and output. Of course, the standard functions of c ++ are not useless, and some functions in stl will soon be used. For example, sort.