题解 | #密码验证合格程序#

密码验证合格程序

https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841

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

bool IsValid(const std::string& passport) {
  if (passport.size() <= 8) {
    return false;
  }
  int has_up_char = 0;
  int has_low_char = 0;
  int has_digit = 0;
  int has_others_char = 0;
  for (const char c : passport) {
    if (c >= 'A' && c <= 'Z') {
      has_up_char = 1;
    } else if (c >= 'a' && c <= 'z') {
      has_low_char = 1;
    } else if (c >= '0' && c <= '9') {
      has_digit = 1;
    } else {
      if (c != ' ' && c != '\n') {
        has_others_char = 1;
      }
    }
  }
  int sum = has_digit + has_low_char + has_others_char + has_up_char;
  if (sum < 3) {
    return false;
  }

  for (int pos = 0; pos < passport.size() - 2; pos++) {
    for (int len = 3; pos + len < passport.size(); len++) {
      std::string str = passport.substr(pos, len);
      if (passport.find(str, pos + len) != std::string::npos) {
        return false;
      }
    }
  }

  return true;
}

int main() {
  std::string passport;
  while (getline(cin, passport)) {
    if (IsValid(passport)) {
      std::cout << "OK" << std::endl;
    } else {
      std::cout << "NG" << std::endl;
    }
  }

  return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

01-16 18:34
四川大学 Java
欢迎加入AI:没有啥稳定不稳定,一切都源于业务快速发展还是收缩。我当年一开始去的央企,业务不赚钱,也贼卷,慢慢就开始优化了。。。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务