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
分享

创作者周榜

更多
正在热议
更多
# 听劝,这个简历怎么改 #
14064次浏览 182人参与
# 面试被问“你的缺点是什么?”怎么答 #
6309次浏览 98人参与
# 水滴春招 #
16228次浏览 341人参与
# 入职第四天,心情怎么样 #
11265次浏览 63人参与
# 租房找室友 #
7997次浏览 53人参与
# 读研or工作,哪个性价比更高? #
26139次浏览 356人参与
# 职场新人生存指南 #
199165次浏览 5506人参与
# 参加完秋招的机械人,还参加春招吗? #
26960次浏览 276人参与
# 文科生还参加今年的春招吗 #
4101次浏览 31人参与
# 简历无回复,你会继续海投还是优化再投? #
48608次浏览 561人参与
# 你见过最离谱的招聘要求是什么? #
144708次浏览 829人参与
# 如果重来一次你还会读研吗 #
155712次浏览 1706人参与
# 机械人选offer,最看重什么? #
69076次浏览 449人参与
# 选择和努力,哪个更重要? #
44261次浏览 492人参与
# 如果再来一次,你还会学硬件吗 #
103638次浏览 1245人参与
# 如果你有一天可以担任公司的CEO,你会做哪三件事? #
20517次浏览 413人参与
# 招聘要求与实际实习内容不符怎么办 #
46662次浏览 494人参与
# 22届毕业,是读研还是拿外包offer先苟着 #
4652次浏览 27人参与
# 你们的毕业论文什么进度了 #
901179次浏览 8960人参与
# 软开人,你觉得应届生多少薪资才算合理? #
81368次浏览 496人参与
# 国企还是互联网,你怎么选? #
109188次浏览 853人参与
牛客网
牛客企业服务