HDU - 5920 Ugly Problem 求解第一个小于n的回文数
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的回文数的更多相关文章
- HDU 5920 Ugly Problem 【模拟】 (2016中国大学生程序设计竞赛(长春))
Ugly Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- HDU 5920 Ugly Problem
说起这道题, 真是一把辛酸泪. 题意 将一个正整数 \(n(\le 10^{1000})\) 分解成不超过50个回文数的和. 做法 构造. 队友UHC提出的一种构造方法, 写起来比较方便一些, 而且比 ...
- HDU 5920 Ugly Problem 高精度减法大模拟 ---2016CCPC长春区域现场赛
题目链接 题意:给定一个很大的数,把他们分为数个回文数的和,分的个数不超过50个,输出个数并输出每个数,special judge. 题解:现场赛的时候很快想出来了思路,把这个数从中间分为两部分,当位 ...
- 2016中国大学生程序设计竞赛(长春)-重现赛 1010Ugly Problem 回文数 模拟
Ugly Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- hdu 1282回文数猜想
http://acm.hdu.edu.cn/showproblem.php?pid=1282 Problem Description 一个正整数,如果从左向右读(称之为正序数)和从右向左读(称之为倒序 ...
- hdu 1282 回文数猜想
Problem Description 一个正整数,如果从左向右读(称之为正序数)和从右向左读(称之为倒序数)是一样的,这样的数就叫回文数.任取一个正整数,如果不是回文数,将该数与他的倒序数相加,若其 ...
- [2014亚马逊amazon] 在线笔试题 大于非负整数N的第一个回文数 Symmetric Number
1.题目 如标题,求大于整数N(N>=0)的第一个回文数的字符串表示形式. 这个题目也是当时笔试第一次见到,花了一个小时才做出了.慢慢总结还是挺简单的. 2.分析 分析如下: (1)一位数N(9 ...
- 大于非负整数N的第一个回文数 Symmetric Number
1.题目 如标题,求大于整数N(N>=0)的第一个回文数的字符串表示形式. 2.样例 1 --> 2 9 -->11 12345 -->12421 123456 --> ...
- LeetCode Problem 9:Palindrome Number回文数
描述:Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
随机推荐
- Maven运行JUnit测试(http://www.360doc.com/content/13/0927/15/7304817_317455642.shtml)
Maven单元测试 分类: maven 2012-05-09 15:17 1986人阅读 评论(1) 收藏 举报 maven测试junit单元测试javarandom 目录(?)[-] maven ...
- poj 2390 Bank Interest(计算本利和)
一.Description Farmer John made a profit last year! He would like to invest it well but wonders how m ...
- HDOJ1059(多重背包)
1.解法一:多重背包 #include<iostream> #include<cstdio> using namespace std; #define MAX(a,b) (a& ...
- VisualGDB系列1:VisualGDB总体概述
根据VisualGDB官网(https://visualgdb.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指正. 本文总体介绍VisualGDB能给你带来 ...
- windows统计端口连接数
netstat -na -p tcp| findstr 80 | find /C "ESTABLISH" netstat -an -p tcp | find "127.0 ...
- sql server 2008 开启1433端口,开启远程连接
通常情况下只需要设置两处
- WPF Canvas
Canvas为容器控件,用于定位. 1.基本应用 <Border HorizontalAlignment="Left" VerticalAlignment="Top ...
- 2017清北学堂(提高组精英班)集训笔记——动态规划Part3
现在是晚上十二点半,好累(无奈脸),接着给各位——也是给自己,更新笔记吧~ 序列型状态划分: 经典例题:乘积最大(Luogu 1018) * 设有一个长度为 N 的数字串,要求选手使用 K 个乘号将它 ...
- [xjtu21]wmq的午餐 计数问题
http://oj.xjtuacm.com/problem/21/ 对13进行分析,每种价格出现的次数: $(C_m^1 + C_m^2 + ... + C_m^m)(C_{n - m}^0 + C_ ...
- Animations使用01 BrowserAnimationsModule
1 基础知识 1.1 动画 就是从一个状态过渡到另外一个状态 1.2 状态 元素本身的形状.颜色.大小等 1.3 Angular中的动画 给元素添加一个触发器,当这个触发器被触发后就会让该元素的状态发 ...