一道模拟题,来模拟进位

暴力的从右往左扫描,按规则求后继就好了。除了Sample已给出的,还有一些需要注意的地方:

  • 9的后继是10,而不是00;
  • (z)的后继是(aa),而不是a(a);
  • 输入虽然最长只有100,但输出最长可能有102。

事实上题目中给的字符串后继规则,也是Ruby中String#succ或String#next所用的规则

http://blog.watashi.ws/1944/the-8th-zjpcpc/

标程也写的非常好

//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0) using namespace std; typedef long long ll ;
typedef unsigned long long ull ;
typedef unsigned int uint ;
typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e- ;
const int N = ;
const int M = * ;
const ll P = 10000000097ll ;
const int MAXN = ; string str;
int n; bool exist () {
for (int i = ; i < str.size(); ++i) {
if (str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' || str[i] >='' && str[i] <= '') return true;
}
return false;
} bool exist (char ch) {
return ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= '' && ch <='';
} int main(){
std::ios::sync_with_stdio(false);
int i, j, t, k, u, v, numCase = ;
int flag; cin >> t;
while (t--) {
cin >> str >> n;
while (n--) {
if (exist ()) {
flag = ;
for (i = str.size () - ; i >= ; --i) {
if (exist (str[i])) {
if (str[i] == '' || str[i] == 'Z' || str[i] == 'z') {
for (j = ; j < i; ++j) {
if (exist (str[j])) break;
}
if (j == i && j != ) { if (str[i] == '') {
flag = ;
str[i] = '';
} else if (str[i] == 'Z') {
flag = ;
str[i] = 'A';
} else if (str[i] == 'z') {
flag = ;
str[i] = 'a';
} if (flag) {
if ( == flag) {
string tmp;
for (k = ; k < i; ++k) tmp.push_back(str[k]);
tmp.push_back('');
for (k = i; k < str.size(); ++k) tmp.push_back(str[k]);
str = tmp;
}
if ( == flag) {
string tmp;
for (k = ; k < i; ++k) tmp.push_back(str[k]);
tmp.push_back('A');
for (k = i; k < str.size(); ++k) tmp.push_back(str[k]);
str = tmp;
}
if ( == flag) {
string tmp;
for (k = ; k < i; ++k) tmp.push_back(str[k]);
tmp.push_back('a');
for (k = i; k < str.size(); ++k) tmp.push_back(str[k]);
str = tmp;
}
}
flag = ; break;
} else {
if (str[i] == '') {
flag = ;
str[i] = '';
} else if (str[i] == 'Z') {
flag = ;
str[i] = 'A';
} else if (str[i] == 'z') {
flag = ;
str[i] = 'a';
}
}
} else {
flag = ;
++str[i]; break;
}
}
} if (flag) {
if ( == flag) {
string tmp;
tmp.push_back('');
for (i = ; i < str.size(); ++i) tmp.push_back(str[i]);
str = tmp;
}
if ( == flag) {
string tmp;
tmp.push_back('A');
for (i = ; i < str.size(); ++i) tmp.push_back(str[i]);
str = tmp;
}
if ( == flag) {
string tmp;
tmp.push_back('a');
for (i = ; i < str.size(); ++i) tmp.push_back(str[i]);
str = tmp;
}
} cout << str << endl;
} else {
++str[str.size () - ];
cout << str << endl;
}
} cout << endl;
} return ;
}

ZOJ 3490 String Successor 字符串处理的更多相关文章

  1. ZOJ 3490 String Successor

    点我看题目 题意 : 给你一个字符串,让你按照给定规则进行处理. 如果字符串里有字母或者是数字就忽略非字符数字,如果没有,就让最右边的那个字符+1. 增量都是从最右边的字母或者数字开始的. 增加一个数 ...

  2. ZOJ 3490 String Successor(模拟)

    Time Limit: 2 Seconds Memory Limit: 65536 KB The successor to a string can be calculated by applying ...

  3. ZOJ——String Successor(字符串模拟题目)

    ZOJ Problem Set - 3490 String Successor Time Limit: 2 Seconds      Memory Limit: 65536 KB The succes ...

  4. .NET面试题解析(03)-string与字符串操作

      系列文章目录地址: .NET面试题解析(00)-开篇来谈谈面试 & 系列文章索引 字符串可以说是C#开发中最常用的类型了,也是对系统性能影响很关键的类型,熟练掌握字符串的操作非常重要. 常 ...

  5. [CareerCup] 1.2 Reverse String 翻转字符串

    1.2 Implement a function void reverse(char *str) in C or C++ which reverses a null-terminated string ...

  6. char型字符串(数组)与string型字符串 指针与引用

    一.常指针: int *const p;    //指针不可改变,但是指针指向的数据可以改变. 指向常量的指针: const int *p;    //指针可以改变,但是指针指向的数据不可以改变. 指 ...

  7. Java基础知识强化59:String(字符串)和其他类型的相互转化

    1. String类型 ---> 其他类型 (1)使用基本类型包装类的parseXXX方法 e.g:String(字符串)转化为int(整型) String MyNumber ="12 ...

  8. String[255]在高版本Delphi里还是被解释成Byte,总体长度256,使用StrPCopy可以给Array String拷贝字符串(内含许多实验测试)

    学了好多不了解的知识: procedure TForm1.Button1Click(Sender: TObject); var s1 : String; s2 : String[]; begin s1 ...

  9. JavaScript String(字符串对象)

    String 对字符串的支持 String.charAt( n ) 返回字符串中的第n个字符 n 是下标 String.charCodeAt( ) 返回字符串中的第n个字符的代码 String.con ...

随机推荐

  1. [LeetCode]题解(python):133-Clone Graph

    题目来源: https://leetcode.com/problems/clone-graph/ 题意分析: 克隆一个无向图.每个节点包括一个数值和它的邻居. 题目思路: 直接深度拷贝. 代码(pyt ...

  2. 读取IOS的相应路径

    //    IOS相应路径 NSString* bundlePath = [[NSBundle mainBundle] bundlePath]; NSLog(@"bundlePath = % ...

  3. VC++ win32 多线程 一边画圆一边画矩形

    // WinThreadTest.cpp : Defines the entry point for the application. // #include "stdafx.h" ...

  4. LintCode-乱序字符串

    题目描述: 给出一个字符串数组S,找到其中所有的乱序字符串(Anagram).如果一个字符串是乱序字符串,那么他存在一个字母集合相同,但顺序不同的字符串也在S中. 注意事项 所有的字符串都只包含小写字 ...

  5. 转: 关于viewport的理解

    最近我做了一点儿针对手机的Web开发和相关研究.按说,Web自设计之初,就已经考虑了设备无关性.然而,现实总是不尽如人意. 我们知道大多数网页都是针对桌面显示器开发和测试的,但是手机屏幕通常要比桌面显 ...

  6. HDU 1294 Rooted Trees Problem

    题目大意:求有n个节点的树有几种? 题解:http://www.cnblogs.com/keam37/p/3639294.html #include <iostream> typedef ...

  7. 表格java代码的相关知识积累

    本文主要收集各大博客中的java表格 用JSP创建一个表格模板 . 项目中要用到一些展示信息的表格,表头不固定,表格内容是即时从后台取的:考虑到复用性,笔者用jsp编写了一个表格模板,可以从reque ...

  8. The Longest Straight(二分,离散化)

     Problem 2216 The Longest Straight Accept: 7    Submit: 14 Time Limit: 1000 mSec    Memory Limit : 3 ...

  9. 【翻译】探究Ext JS 5和Sencha Touch的布局系统

    原文:Exploring the Layout System in Ext JS 5 and Sencha Touch 布局系统是Sencha框架中最强大和最有特色的一个部分. 布局要处理应用程序中每 ...

  10. Live Writer Test

    测试下LiveWriter写CNblog: 1.Source code plug-in: @Override public List getAll(String orgid,String start, ...