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的?
#华为##笔试题目##题解#