题解 | 小红的对称串

小红的对称串

https://www.nowcoder.com/practice/9719e6db0bbd41b6bbf5c8052430b1f5

//  #牛客春招刷题训练营# https://www.nowcoder.com/discuss/726480854079250432
//  模拟题,详细请见代码注释
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
map<char, int> cnt;
bool check(string s){//----------------检查除中间之外的值是否轴对称
  int i = 0, j = s.size() - 1;
  int endpoint = s.size() / 2;
  while(i < endpoint){
    if (s[i] != s[j] || s[i] == 'b' || s[i] == 'q' || s[i] == 'd' || s[i] == 'p'){//------这里注意b、q、d、p的特判
      int ls = cnt[s[i]] + cnt[s[j]];
      if (ls != 3 && ls != 7) return 1;//--------这里我为了省力用了数值,也可以一个一个判断只是要写很多字
    }
    i++;
    j--;
  }
  return 0;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  map<char, bool> ma;
  string s = "ilmnouvwx";
  for (int i = 0; i < s.size(); i++){
    ma[s[i]] = true;
  }
  #define yy "Yes"//--------为了少打一点字
  #define nn "No"
  cnt['q'] = 1;
  cnt['p'] = 2;
  cnt['b'] = 3;
  cnt['d'] = 4;
  for (int i = 0; i < n; i++){
    cin >> s;
    if (check(s)){//-------------如果周边部分有非轴对称,则nn
      cout << nn << endl;
    }
    else {
      if (s.size() & 1){//------------奇数判断中间
        if (ma[s[s.size() / 2]]){//------用了map标记
          cout << yy << endl;
        }
        else {
          cout << nn << endl;
        }
      }
      else {//----------偶数,yy
        cout << yy << endl;
      }
    }
  }
}
// 64 位输出请用 printf("%lld")

#写题解领奖励##牛客春招刷题训练营#
全部评论

相关推荐

11-23 15:14
中原工学院 Java
程序员花海_:实习和校招简历正确格式应该是教育背景+实习+项目经历+个人评价 其中项目经历注意要体现业务 实习经历里面的业务更是要自圆其说 简历模板尽可能保持干净整洁 不要太花哨的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务