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. CentOS 6.X 安装 EPEL 源

    CentOS 6.X 自带的软件源可选的并不多,有时候要找到一个偏门一些的软件,用命令一搜怎么都没有源,考虑到使用软件源配合 yum 命令安装可以自动安装依赖,所以加一个新的软件源迫在眉睫. 考虑到同 ...

  2. Python 二维列表

    一维列表,可以使用 * 快速创建list1=[0]*Width r = [0]*5 print r r[1]= 1 print r [0, 0, 0, 0, 0] [0, 1, 0, 0, 0] 扩展 ...

  3. <正则吃饺子> :关于mybatis中使用的问题(一)

    在公司项目开始之前,根据springboot .mybatis.Swagger2 整合了一个demo,在测试时候,遇到的问题,简单记录.之前在使用mybatis时候,没有注意到这一点. 1.错误:Th ...

  4. SVN使用技巧和参考文档总结

    以下文章为网上收集: myEclipse 8.5下SVN环境的搭建(重点推荐) SVN建立版本库,配置用户和权限 Tortoise SVN使用方法,简易图解 版本控制软件SVN使用方法详解 学习笔记 ...

  5. [解决问题]ubuntu无法virtualenv创建python虚拟环境的解决

    刚有人问我Ubuntu python虚拟环境无法创建问题,报错same file error,防止今后遇到忘记,记录下可能的问题. 1.先在windows上试了下: pip install virtu ...

  6. Zabbix_proxy的架设

    一.安装zabbix-proxy与导入数据库 1. 安装 zabbix-server $ sudo rpm -ivh http://repo.zabbix.com/zabbix/3.0/rhel/7/ ...

  7. 第七篇 elasticsearch 链接mysql不会更新

    这是我键的索引 "settings":{ "number_of_shards":3, "number_of_replicas":2 }, & ...

  8. windows7 8 mac 安装docker

    安装: 下载 DockerToolbox.exe 苹果的也可以用这个链接下载 一直点击下一步(网络要求很高安装时,网络不好会报错 多点几次就好了)

  9. Blast 如何使用Blast+(Linux)转载

    下载数据 #此处下载对应物种的数据库ftp://ftp.ncbi.nih.gov/genomes/,下载fna格式的即可   wget ftp://ftp.ncbi.nih.gov/genomes/A ...

  10. p2341&bzoj1051 受欢迎的牛

    传送门(洛谷) 传送门(bzoj) 题目 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种关系是具有传递性的,如果A认为B受欢迎,B认为C ...