牛客:HJ20 密码验证合格程序[华为机考][字符串]
学习要点
- 利用容器本身的功能来为解题提供服务
- string.find
- string.substr
- 范围for
- string::npos
题目链接
密码验证合格程序_牛客题霸_牛客网
题目描述
解法:字符串操作
#include <bits/stdc++.h>
#include <cctype>
#include <iostream>
using namespace std;int main() {// 接收字符串vector<string> my_print;string line_str;while(getline(cin,line_str)){bool X = false; bool x = false; bool number = false; bool ch = false;int n = line_str.size();if(n < 8){my_print.push_back("NG");continue;}for(auto &i:line_str){if(islower(i)){x = true;}else if(isupper(i)){X = true;}else if(i>='0' && i<='9'){number = true;}else {ch = true;}}int count =0;if(X) count++;if(x) count++;if(number) count++;if(ch) count++;if(count <3){my_print.push_back("NG");continue;}for(int i =0;i<n;i++){if((i+4) >= n){my_print.push_back("OK");break;}string tmp_str = line_str.substr(i,3);int pos = line_str.find(tmp_str,i+3);if(pos != string::npos){my_print.push_back("NG");// cout << 1 << endl;break;}}}for(auto& str:my_print){cout << str << endl;}}
// 64 位输出请用 printf("%lld")