4.24华为笔试第一题

/**
    题目:判断输入的字符串是否为双对称字符串,是的话输出对应的单对称字符串(aabbaa则输出aba),否则输出false。
        双对称字符串:
            1、正序逆序相同。
            2、偶数个字符.
            3、从第一个字符开始每一对字符相同(aabbccbbaa)。
*/

#include <iostream>
using namespace std;
#include <string>

void printStr(const string& str)
{
    int strLen = str.size();
    for(int i = 0; i < strLen; i += 2){
        cout << str[i];
    }
    cout << endl;
}

int main()
{
    int strLen = 0, i = 0, j = 0;
    bool isPass = false;
    string str;
    while(cin >> str){
        strLen = str.size();
        if(strLen < 2 || strLen & 1 == 1){
            cout << "false" << endl;
            continue;
        }else{
            int i = 0, j = strLen - 1;
            while(i < j){
                if(str[i] == str[i + 1] && str[j] == str[j - 1] && str[i] == str[j]){
                    i += 2;
                    j -= 2;
                    isPass = true;
                }else{
                    isPass = false;
                    break;
                }
            }
            if(isPass == true){
                printStr(str);
                continue;
            }else{
                cout << "false" << endl;
                continue;
            }
        }
    }
    return 0;
}
我的方法是设置头尾两个指针往中间夹并进行比较,但是这个代码AC不了(本地测试结果是对的),提示的超时,应该是因为我程序一直循环输入的原因?但是题目描述会有多个字符串输入,最后我只能改成只能输入一个字符串,结果通过60%,想知道大佬们怎么AC的?
#华为##笔试题目##题解#
全部评论
这个可以理解为先判断是不是回文字符串么
点赞 回复
分享
发布于 2019-04-24 23:45
笔试多少算过啊
点赞 回复
分享
发布于 2019-04-24 23:55
博乐游戏
校招火热招聘中
官网直投
我也60%
点赞 回复
分享
发布于 2019-04-24 23:55
同60%
点赞 回复
分享
发布于 2019-04-24 23:56
老哥第三题 怎么判断顺序的 可以知道哪些数字 全a需要数字的排序也是正确的吗
点赞 回复
分享
发布于 2019-04-25 00:04
三步判断 1 判断长度是否为偶数 2判断逆序相同,直接反转然后比较两个字符串 3 判断每对字符相同,即aa bb cc这样的去判断
点赞 回复
分享
发布于 2019-04-25 00:08
#coding=utf-8 import sys for line in sys.stdin: a = line.split()[0] # print(a) a=list(a.lower()) output='' # print(a) eight_num=a.count('g') output+='8'*eight_num for i in range(eight_num): a.pop(a.index('e')) a.pop(index=a.index('i')) a.pop(index=a.index('g')) a.pop(index=a.index('h')) a.pop(index=a.index('t')) six_num=a.count('x') output += '6' * six_num for i in range(six_num): a.pop(a.index('s')) a.pop(a.index('i')) a.pop(a.index('x')) seven_num = a.count('s') output += '7' * seven_num for i in range(seven_num): a.pop(index=a.index('s')) a.pop(index=a.index('e')) a.pop(index=a.index('v')) a.pop(index=a.index('e')) a.pop(index=a.index('n')) five_num = a.count('v') output += '5' * five_num for i in range(seven_num): a.pop(index=a.index('f')) a.pop(index=a.index('i')) a.pop(index=a.index('v')) a.pop(index=a.index('e')) four_num = a.count('v') output += '4' * four_num for i in range(four_num): a.pop(index=a.index('f')) a.pop(index=a.index('o')) a.pop(index=a.index('u')) a.pop(index=a.index('r')) two_num = a.count('w') output += '2' * two_num for i in range(two_num): a.pop(index=a.index('t')) a.pop(index=a.index('w')) a.pop(index=a.index('o')) three_num = a.count('t') output += '3' * three_num for i in range(three_num): a.pop(index=a.index('t')) a.pop(index=a.index('h')) a.pop(index=a.index('r')) a.pop(index=a.index('e')) a.pop(index=a.index('e')) nine_num = a.count('i') output += '9' * nine_num for i in range(nine_num): a.pop(index=a.index('n')) a.pop(index=a.index('i')) a.pop(index=a.index('n')) a.pop(index=a.index('e')) one_num = a.count('o') output += '1' * one_num for i in range(one_num): a.pop(index=a.index('o')) a.pop(index=a.index('n')) a.pop(index=a.index('e')) print(sorted(output)) 为啥只能AC 50%,求大佬帮忙看看
点赞 回复
分享
发布于 2019-04-25 14:26

相关推荐

点赞 32 评论
分享
牛客网
牛客企业服务