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. CF1092 D & E —— 思路+单调栈,树的直径

    题目:https://codeforces.com/contest/1092/problem/D1 https://codeforces.com/contest/1092/problem/D2 htt ...

  2. IPC的使用

    IPC,Inter-Processor Communication是SYS/BIOS处理核间通信的组件: IPC的几种应用方式: 1.最小使用(Minimal use) 这种情况是通过核间的通知机制( ...

  3. JavaScript-Tool:pluload

    ylbtech-JavaScript-Tool:pluload Plupload是用于处理文件上传的JavaScript API,支持多文件选择.文件类型过滤.请求分块.客户端图像缩放等功能,使用不同 ...

  4. PWA PSI statusingclient.UpdateStatus更新任务页面的AssnCustomFields的TextValue值

    1.注意Changesxml格式和下面一定要一样 2.CustomFieldGuid和CustomFieldName都不能少,自定义域的uid和name其中uid或者是MD_PROP_UID_SECO ...

  5. LAMP 1.6 Discuz打开错误

    打开discuz失败, ps aux |grep mysql ps aux |grep httpd 查看mysql apache有没有打开. 重启mysql service mysqld restar ...

  6. 为组件设定UI

    -----------------siwuxie095                             工程名:CustomizeSwing 包名:com.siwuxie095.swing 类 ...

  7. SpringBoot05 数据操作03 -> JPA查询方法的规则定义

    请参见<springboot详解>springjpa部分知识 1 按照方法命名来进行查询 待更新... package cn.xiangxu.springboot.repository; ...

  8. 转:基于InfluxDB&Grafana的JMeter实时性能测试数据的监控和展示

    本文主要讲述如何利用JMeter监听器Backend Listener,配合使用InfluxDB+Grafana展示实时性能测试数据 关于JMeter实时测试数据 JMeter从2.11版本开始,命令 ...

  9. ps -ef | grep java 输出的具体含义是什么?

      uid        pid    ppid # PID 这个程序的 ID 下面的 PPID 则是父程序的 ID: PS是LINUX下最常用的也是非常强大的进程查看命令 常见的使用方法是检查一个进 ...

  10. UE4简单实现描边高亮效果

    材质文件下载地址: 链接:https://pan.baidu.com/s/10HUmXR_YNMOTF-Cg4ybuUg 提取码:m1my 1. 将材质文件放到Content目录中 2. 在项目中添加 ...