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. Linux不停往外发包

    一台Linux这两天不停往外发包,造成外部无法访问. [root@ct-nat ~]# watch ifconfig-------------查看数据包新增情况 Every 2.0s: ifconfi ...

  2. JAVA 1.5 并发之 Executor框架 (二)execute VS submit

    http://www.cnblogs.com/rockman12352/p/3788688.html 上一篇对于整体框架讲了很多东西,但是具体在使用时有一些细节并没有说出来 首先是执行任务 execu ...

  3. IOS+openCV在Xcode的入门开发

    昨天折腾了一天,终于搞定了openCV+IOS在Xcode下的环境并且实现一个基于霍夫算法的圆形识别程序.废话不多说,下面就是具体的折腾流程: ---------------------------- ...

  4. Unity编辑器的扩展:IMGUI

    IMGUI 介绍 所有关于 Editor 的相关 UI,包括 Inspector.Hierarchy.Window.Game 视图上动态创建的那些半透明 UI.还有 Scene 视图上可添加的辅助显示 ...

  5. 转——AHRS(航姿参考系统)和IMU(惯性测量单元)的区别

    AHRS(航姿参考系统)和IMU(惯性测量单元)的区别  [测试测量] 发布时间:2010-05-09 16:52:09  http://bbs.ednchina.com/BLOG_ARTICLE_1 ...

  6. Ubuntu 12.04 LTS为例演示vsftpd虚拟用户 的配置

    vsftpd虚拟用户   2012-05-19 15:46:59|  分类: GNU/Linux |  标签:ubuntu  vsftpd  ftp  虚拟用户  |举报|字号 订阅 我们登录FTP有 ...

  7. 【QtAV】QtAV中的工厂模式

    QtAV中的各个模块大量使用的工厂模式,下面对其实现进行介绍. 工厂模式的使用 以 VideoRenderer 类为例子,他含有下面3个工厂模式相关的方法,Register方法用于给一个产品<c ...

  8. springboot 使用war包部署

    ps://www.cnblogs.com/jiaoyiping/p/4251718.html

  9. <富爸爸,穷爸爸> 书中的好句子

    成为 做 拥有 资产是能把钱放进你口袋里的东西:负债是把钱从你的口袋里取走的东西. 语言会变成血肉,留在我们的身体里 要想做一个成功的投资者,你必须在情感上对赚钱和赔钱漠不关心,赚钱和赔钱只是游戏的一 ...

  10. (扫码二维码不显示)php微信扫码支付sdk不能用了

    解决方案: img标签中src 该为qrcode.php的绝对路径,如: 备注:微信端这个生成二维码的路径老是改,之前是http://paysdk.weixin.qq.com/example/qrco ...