ZOJ,zoj題目分類

來源:互聯網
上載者:User

ZOJ,zoj題目分類

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 number generalized 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 integer N, please find the largest generalized palindromic number less thanN.

Input

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

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

Output

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

Sample Input

41212312241122

Sample Output

1112112211121題意:求小於N的迴文數,這個數的迴文相同的數可以縮成一個數思路:dfs(l, r, eq):l代表左邊側長度,r代表右邊的長度,eq代表是否處於邊界,然後在搜尋右邊匹配左邊的同時,枚舉k,k代表連續相同的數都匹配左邊的個數
#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

我做了好長時間才AC……
其實tle不是你的程式有問題,而是確實是你程式在啟動並執行過程中逾時了。就是說你程式啟動並執行很慢,結果最後超過1秒了就判定逾時。

逾時有很多原因,有的是演算法的問題,但是你的程式沒演算法問題。所以只能是另外的一種可能,就是輸入輸出的問題。

你使用的c++標準輸入輸出cin cout 還有標準字串類string,這些都是c++的對象或類,在使用的時候都要建立對象,使用起來速度很慢。在對小規模的acm問題時不影響,但是對輸入量非常大的時候,使用cin cout明顯比scanf和printf要慢的多。
string也是比vector<char>慢,而vector<char>又比char[]慢。。。

舉個例子,我曾經做一個題目,用scanf和printf用的0ms,而用cin和cout(沒改任何地方)用了700多ms……

我是用你的程式模板改成了char[]並把輸入輸出改了gets,printf,scanf等函數後AC的。

希望做ACM還是養成良好的習慣,輸入輸出盡量用C的函數吧,當然c++的標準函數也不是一無是處,stl裡面的有些函數就很快。比如sort等。
 
zoj 1151

我做了好長時間才AC……
其實tle不是你的程式有問題,而是確實是你程式在啟動並執行過程中逾時了。就是說你程式啟動並執行很慢,結果最後超過1秒了就判定逾時。

逾時有很多原因,有的是演算法的問題,但是你的程式沒演算法問題。所以只能是另外的一種可能,就是輸入輸出的問題。

你使用的c++標準輸入輸出cin cout 還有標準字串類string,這些都是c++的對象或類,在使用的時候都要建立對象,使用起來速度很慢。在對小規模的acm問題時不影響,但是對輸入量非常大的時候,使用cin cout明顯比scanf和printf要慢的多。
string也是比vector<char>慢,而vector<char>又比char[]慢。。。

舉個例子,我曾經做一個題目,用scanf和printf用的0ms,而用cin和cout(沒改任何地方)用了700多ms……

我是用你的程式模板改成了char[]並把輸入輸出改了gets,printf,scanf等函數後AC的。

希望做ACM還是養成良好的習慣,輸入輸出盡量用C的函數吧,當然c++的標準函數也不是一無是處,stl裡面的有些函數就很快。比如sort等。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.