http://acm.hdu.edu.cn/showproblem.php?pid=5920

http://www.cnblogs.com/xudong-bupt/p/4015226.html

把前半部分复制过去,如果太大,那么早到第一个会使得其太大的点,减1,然后对应的中间的变成9

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
#define X first
#define Y second
#define clr(u,v); memset(u,v,sizeof(u));
#define in() freopen("3.h","r",stdin);
#define out() freopen("ans","w",stdout);
#define Clear(Q); while (!Q.empty()) Q.pop();
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const ll INF = 1e17;
const int inf = 0x3f3f3f3f;
const int maxn = 1e3 + ;
char nxt[maxn], str[maxn];
bool is(char str[], int lenstr) {
int i = , j = lenstr;
while (i < j) {
if (str[i] != str[j]) return false;
++i;
--j;
}
return true;
}
void bigCut(char str[], char sub[], char str3[]) {
int lenstr = strlen(str + ), lensub = strlen(sub + );
// printf("%d\n", lenstr);
for (int i = ; i <= lenstr; ++i) str[i] -= '';
for (int i = ; i <= lensub; ++i) sub[i] -= '';
int use = lenstr;
for (int i = lensub; i >= ; --i, --use) {
if (str[use] < sub[i]) {
str[use] = + str[use] - sub[i];
int p = use - ;
while (p >= && !str[p]) {
str[p] = ;
p--;
}
str[p]--;
} else str[use] -= sub[i];
}
for (int i = ; i <= lenstr; ++i) str[i] += '';
int to = ;
int p = ;
while (p < lenstr && str[p] == '') p++;
while (p <= lenstr) {
str3[++to] = str[p++];
}
str3[++to] = '\0';
}
char s2[];
void findNxt(char str[], int lenstr) {
if (is(str, lenstr)) {
strcpy(nxt + , str + );
return;
}
if (str[] == '') {
bool flag = true;
for (int i = ; i <= lenstr; ++i) {
if (str[i] != '') {
flag = false;
break;
}
}
if (flag) {
for (int i = ; i <= lenstr - ; ++i) nxt[i] = '';
nxt[lenstr] = '\0';
return;
}
}
for (int i = ,j=lenstr; i <=j; ++i,--j) {
s2[j] = s2[i] = str[i];
}
for (int i = lenstr / + ; i <= lenstr; ++i) {
if (str[i] < s2[i]) {
for(int j=(lenstr+)/;;j--)
{
if(s2[j]=='')
{
s2[j]='';
s2[lenstr-j+]=s2[j];
}
else
{
s2[j]--;
s2[lenstr-j+]=s2[j];
break;
}
}
break;
}
}
for (int i = ; i <= lenstr; ++i) nxt[i] = s2[i];
nxt[lenstr + ] = '\0';
} char res[];
char sub[] = "";
vector<string> vc;
int f;
void work() {
printf("Case #%d:\n", ++f);
vc.clear();
scanf("%s", str + );
int lenstr = strlen(str + );
// findNxt(str, lenstr);
// printf("%s\n", nxt + 1);
while (true) {
findNxt(str, lenstr);
if (strcmp(str + , nxt + ) == ) {
if (is(str, lenstr)) {
vc.push_back(str + );
break;
} else {
vc.push_back("");
sub[] = '';
sub[] = '';
sub[] = '\0';
bigCut(str, sub, res);
strcpy(str + , res + );
lenstr = strlen(str + );
continue;
}
}
vc.push_back(nxt + );
bigCut(str, nxt, res);
strcpy(str + , res + );
lenstr = strlen(str + );
}
printf("%d\n", vc.size());
for (int i = ; i < vc.size(); ++i) {
printf("%s\n", vc[i].c_str());
}
} int main()
{
#ifdef LOCAL
in();
#else
#endif
int t;
scanf("%d", &t);
while (t--) work();
return ;
}

贪心的思路是:最好不要更改前半部分,权值大。

HDU - 5920 Ugly Problem 求解第一个小于n的回文数的更多相关文章

  1. HDU 5920 Ugly Problem 【模拟】 (2016中国大学生程序设计竞赛(长春))

    Ugly Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  2. HDU 5920 Ugly Problem

    说起这道题, 真是一把辛酸泪. 题意 将一个正整数 \(n(\le 10^{1000})\) 分解成不超过50个回文数的和. 做法 构造. 队友UHC提出的一种构造方法, 写起来比较方便一些, 而且比 ...

  3. HDU 5920 Ugly Problem 高精度减法大模拟 ---2016CCPC长春区域现场赛

    题目链接 题意:给定一个很大的数,把他们分为数个回文数的和,分的个数不超过50个,输出个数并输出每个数,special judge. 题解:现场赛的时候很快想出来了思路,把这个数从中间分为两部分,当位 ...

  4. 2016中国大学生程序设计竞赛(长春)-重现赛 1010Ugly Problem 回文数 模拟

    Ugly Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  5. hdu 1282回文数猜想

    http://acm.hdu.edu.cn/showproblem.php?pid=1282 Problem Description 一个正整数,如果从左向右读(称之为正序数)和从右向左读(称之为倒序 ...

  6. hdu 1282 回文数猜想

    Problem Description 一个正整数,如果从左向右读(称之为正序数)和从右向左读(称之为倒序数)是一样的,这样的数就叫回文数.任取一个正整数,如果不是回文数,将该数与他的倒序数相加,若其 ...

  7. [2014亚马逊amazon] 在线笔试题 大于非负整数N的第一个回文数 Symmetric Number

    1.题目 如标题,求大于整数N(N>=0)的第一个回文数的字符串表示形式. 这个题目也是当时笔试第一次见到,花了一个小时才做出了.慢慢总结还是挺简单的. 2.分析 分析如下: (1)一位数N(9 ...

  8. 大于非负整数N的第一个回文数 Symmetric Number

    1.题目 如标题,求大于整数N(N>=0)的第一个回文数的字符串表示形式. 2.样例 1  --> 2 9  -->11 12345 -->12421 123456 --> ...

  9. LeetCode Problem 9:Palindrome Number回文数

    描述:Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...

随机推荐

  1. asp.net分页asp.net无刷新分页高效率分页

    项目中经常会用到分页的功能类似的项目做过无数个了,今个把自己常用的分页代码分享一下. 首先说说服务端处理的代码: 下面代码中重点是分页的sql语句的写法,其中的参数@n是当前的页码,总的来说本服务端主 ...

  2. Ubuntu下安装软件

    在ubuntu当中,安装应用程序有三种方法,分别是:apt-get,dpkg安装deb和make install安装源码包三种. apt-get方法 使用apt-get install来安装应用程序算 ...

  3. 四 Synchronized

    首先,一个问题:一个boolean成员变量,一个方法赋值,一个方法读值,多线程环境下,需要同步吗? 如果用同步的话,读也要用synchroized修饰,因为可见性的问题 需要同步,或者用volatil ...

  4. JavaScript原型模式(prototype)

    1.原型是一个对象,其他对象可以通过它实现属性的继承所有对象在默认的情况下都有一个原型,因为原型的本身也是对象,所以一个类的真正原型是被类的内部[prototype]属性所指出.每个函数都有一个属性叫 ...

  5. webrower + CEF

    理解WebKit和Chromium: Content API和CEF3 标签:               apiAPIAPibrowserchromeChromehtml5HTML5Html5web ...

  6. shell入门-awk-3

    awk的内置变量 NR 表示行 NF 表示段 显示第十行 [root@wangshaojun ~]# awk -F ':' 'NR==10' 1.txtuucp:x:10:14:uucp:/var/s ...

  7. hibernate.PropertyAccessException: Null value was assigned to a property of primitive type

    日志: [WARN-2016/07/26/18/:45/:52]ProcessEngineServiceImpl.(257) - 审批流程log日志--submitProcess方法:提交人userI ...

  8. hadoop mapreduce 计算平均气温的代码,绝对原创

    1901 46 1902 21 1903 48 1904 33 1905 43 1906 47 1907 31 1908 28 1909 26 1910 35 1911 30 1912 16 1913 ...

  9. 选择炸了(JIRA)的88个

    作者:Martin Seibert SEIBERT MEDIA 首席执行官. 原文地址:http://seibert.biz/jirareasons 作者Martin Seibert 是德国互联网代理 ...

  10. leetcode-002

    给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...