leetcode/nowcoder-huawei-2-字符串操作

2.字符串操作

HJ17 坐标移动

  1. 描述

    开发一个坐标计算工具, A表示向左移动,D表示向右移动,W表示向上移动,S表示向下移动。从(0,0)点开始移动,从输入字符串里面读取一些坐标,并将最终输入结果输出到输出文件里面。
    输入:
    合法坐标为A(或者D或者W或者S) + 数字(两位以内)
    坐标之间以;分隔。
    非法坐标点需要进行丢弃。如AA10;  A1A;  $%$;  YAD; 等。
    下面是一个简单的例子 如:
    A10;S20;W10;D30;X;A1A;B10A11;;A10;
    处理过程:
    起点(0,0)
    +   A10   =  (-10,0)
    +   S20   =  (-10,-20)
    +   W10  =  (-10,-10)
    +   D30  =  (20,-10)
    +   x    =  无效
    +   A1A   =  无效
    +   B10A11   =  无效
    +  一个空 不影响
    +   A10  =  (10,-10)
    结果 (10, -10)
    
    数据范围:每组输入的字符串长度满足 1≤n≤10000  ,坐标保证满足 -2^{31}≤x,y≤2^{31}-1,且数字部分仅含正数
    
    输入描述:
    一行字符串
    输出描述:
    最终坐标,以逗号分隔
    
    示例1
    输入:A10;S20;W10;D30;X;A1A;B10A11;;A10;
    输出:10,-10
    
    示例2
    输入:ABC;AKL;DA1;
    输出:0,0
    
  2. 思路

    获取所有操作部分方向和移动距离,压入队列,最后再遍历结果

  3. 代码

    #include <iostream>
    using namespace std;
    #include <vector>
    
    bool isNum(char ch) {
        int num = ch - '0';
        return num >= 0 && num < 10;
    }
    
    pair<int,int> isOp(char ch) {
        switch (ch) {
            case 'A': return {-1, 0};
            case 'S': return { 0,-1};
            case 'W': return { 0, 1};
            case 'D': return { 1, 0};    
            default:  return { 0, 0};
        }
        return { 0, 0};
    }
    
    pair<int, int> operation(string str) {
        int fpos = 0;
        int epos = 0;
        vector<pair<int, int>> loc;
        loc.push_back({0, 0});
        cout << str.size() << endl;
        int len = 0;
        int num = 0;
        int s = 0;
        while((epos < str.size()) && (epos = str.find(';', epos))) {
            len = epos - fpos;
            auto [x, y] = isOp(str[fpos]);
            s = fpos;
            fpos = (epos++) + 1;
            if ((!x && !y) || len > 3 || len < 2 || !isNum(str[s+1])) continue;
            num = str[++s] - '0';
            if (len == 3 && !isNum(str[s+1]))   continue;
            if (len == 2) {
                loc.push_back({x * num, y * num});
                continue;
            }
            num = num * 10 + (str[++s] - '0');
            cout << num <<"  "<< epos << "   " << s << endl;
            loc.push_back({x * num, y * num});
        }
        int a = 0;
        int b = 0;
        for (auto [x, y]:loc) {
            printf("%d , %d \n",x,y);
            a += x;
            b += y;
        }
            printf("\n\n%d , %d \n",a,b);
        return {a , b};
            
    }
    
    int main() {
        string str;
        cin >> str;
        cout << str << endl;
        operation(str);
        return 0;
    }
    

HJ20. 密码验证合格程序

  1. 描述

    密码要求:
    1.长度超过8位
    2.包括大小写字母.数字.其它符号,以上四种至少三种
    3.不能有长度大于2的包含公共元素的子串重复 (注:其他符号不含空格或换行)
    
    数据范围:输入的字符串长度满足 1≤n≤100 
    输入描述:
    一组字符串。
    
    输出描述:
    如果符合要求输出:OK,否则输出NG
    
    示例1
    输入:
        021Abc9000
        021Abc9Abc1
        021ABC9000
        021$bc9000
    输出:
        OK
        NG
        NG
        OK
    
  2. 思路

    先判断长度,再判断类型。将所有长度为3的字符串放入map,如果有重复的直接返回false

  3. 代码

    #include <iostream>
    using namespace std;
    #include <map>
    
    bool isRight(string str) {
        int n = str.length();
        if (n < 8 || n > 100)       return false;
        map<string, int> strs;
        int isSmallLetter = 0;
        int isLargeLetter = 0;
        int isNum = 0;
        int isOther = 0;
        string tmp = "";
        for(int i = 0; i < n; i++) {
            char ch = str[i];
            cout << ch << endl;
            printf("%d  %d   %d\n",i+1,i+2,n);
            if (i + 1 < n && i + 2 < n) {
                tmp.clear();
                tmp.push_back(str[i]);
                tmp.push_back(str[i+1]);
                tmp.push_back(str[i+2]);
                printf("%c%c%c  ",str[i],str[i+1],str[i+2]);
                cout << tmp << endl;
                if(strs[tmp])   return false;
                strs[tmp]++;
            }
            if ((ch - 'a') >= 0 && (ch - 'a') <= 26)        isSmallLetter = 1;
            else if ((ch - 'A') >= 0 && (ch - 'A') <= 26)   isLargeLetter = 1;
            else if ((ch - '0') >= 0 && (ch - '0') <= 9)    isNum = 1;
            else    isOther = 1;
        }
        if((isSmallLetter + isLargeLetter + isNum + isOther) < 3)   return false;
        return true;
    }
    
    int main() {
        string str;
        while(cin >> str) {
            bool res = isRight(str);
            if(res) cout << "OK" << endl;
            else    cout << "NG" << endl;
        }
        return 0;
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值