• ZOJ 3490 String Successor 字符串处理


    一道模拟题,来模拟进位

    暴力的从右往左扫描,按规则求后继就好了。除了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-7      ;
    const int N = 210            ;
    const int M = 1100011*2      ;
    const ll P = 10000000097ll   ;
    const int MAXN = 10900000    ;
    
    string str;
    int n;
    
    bool exist () {
        for (int i = 0; i < str.size(); ++i) {
            if (str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' || str[i] >='0' && str[i] <= '9')    return true;
        }
        return false;
    }
    
    bool exist (char ch) {
        return ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= '0' && ch <='9';
    }
    
    int main(){
        std::ios::sync_with_stdio(false);
        int i, j, t, k, u, v, numCase = 0;
        int flag;
    
        cin >> t;
        while (t--) {
            cin >> str >> n;
            while (n--) {
                if (exist ()) {
                    flag = 0;
                    for (i = str.size () - 1; i >= 0; --i) {
                        if (exist (str[i])) {
                            if (str[i] == '9' || str[i] == 'Z' || str[i] == 'z') {
                                for (j = 0; j < i; ++j) {
                                    if (exist (str[j])) break;
                                }
                                if (j == i && j != 0) {
    
                                    if (str[i] == '9')  {
                                        flag  = 1;
                                        str[i] = '0';
                                    } else if (str[i] == 'Z') {
                                        flag  = 2;
                                        str[i] = 'A';
                                    } else if (str[i] == 'z') {
                                        flag  = 3;
                                        str[i] = 'a';
                                    }
    
                                    if (flag) {
                                        if (1 == flag) {
                                            string tmp;
                                            for (k = 0; k < i; ++k) tmp.push_back(str[k]);
                                            tmp.push_back('1');
                                            for (k = i; k < str.size(); ++k) tmp.push_back(str[k]);
                                            str = tmp;
                                        }
                                        if (2 == flag) {
                                            string tmp;
                                            for (k = 0; 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 (3 == flag) {
                                            string tmp;
                                            for (k = 0; 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 = 0;
    
                                    break;
                                }
    
                                else {
                                    if (str[i] == '9')  {
                                        flag  = 1;
                                        str[i] = '0';
                                    } else if (str[i] == 'Z') {
                                        flag  = 2;
                                        str[i] = 'A';
                                    } else if (str[i] == 'z') {
                                        flag  = 3;
                                        str[i] = 'a';
                                    }
                                }
                            }
    
                            else {
                                flag = 0;
                                ++str[i];
    
                                break;
                            }
                        }
                    }
    
                    if (flag) {
                        if (1 == flag) {
                            string tmp;
                            tmp.push_back('1');
                            for (i = 0; i < str.size(); ++i) tmp.push_back(str[i]);
                            str = tmp;
                        }
                        if (2 == flag) {
                            string tmp;
                            tmp.push_back('A');
                            for (i = 0; i < str.size(); ++i) tmp.push_back(str[i]);
                            str = tmp;
                        }
                        if (3 == flag) {
                            string tmp;
                            tmp.push_back('a');
                            for (i = 0; i < str.size(); ++i) tmp.push_back(str[i]);
                            str = tmp;
                        }
                    }
    
                    cout << str << endl;
                } else {
                    ++str[str.size () - 1];
                    cout << str << endl;
                }
            }
    
            cout << endl;
        }
    
        return 0;
    }
  • 相关阅读:
    SharedPreferences介绍,用来做数据存储
    android中的回调简单认识
    git的使用
    Android Studio插件美化Android Studio,文艺清新范
    arp欺骗技术
    进程和线程的关系
    Win下常用命令大全
    JavaWeb系列之:Servlet
    JavaWeb系列之:监听器
    JavaWeb系列之:过滤器
  • 原文地址:https://www.cnblogs.com/wushuaiyi/p/4410063.html
Copyright © 2020-2023  润新知