题解 | 小红的对称串

小红的对称串

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")

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

相关推荐

07-22 13:50
门头沟学院 Java
仁者伍敌:其实能找到就很好了,当然收支能抵
点赞 评论 收藏
分享
06-04 09:27
门头沟学院 Java
点赞 评论 收藏
分享
07-02 10:39
门头沟学院 Java
Steven267:说点真实的,都要秋招了,还没有实习,早干嘛去了,本来学历就差,现在知道急了,而且你这个简历完全可以写成一页,劣势太大了,建议转测试
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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