[华为机试]密码验证合格程序(字符串)

题目链接
分析:主要就是判断字符串内不能有长度超过2的重复字串。
那么就搜索看是不是存在长度为3的重复字串,如果没有就ok。
字符串函数 s . s u b s t r ( i , l e n ) s.substr(i,len) s.substr(i,len)熟悉一下,表示从字符串s的下标i处截取长度为len的字串。


#include<iostream>
#include<cstring>
using namespace std;
string s;
int main(){
    std::ios::sync_with_stdio(false);
    while(cin>>s){
        int len=s.length();
        if(len<=8) {cout<<"NG"<<endl;}
        else{
            int ans1=0,ans2=0,ans3=0,ans4=0;
            for(int i=0;i<len;i++){
                if(s[i]>='a'&&s[i]<='z') {ans1=1;}
                else if(s[i]>='A'&&s[i]<='Z') {ans2=1;}
                else if(s[i]>='0'&&s[i]<='9') {ans3=1;}
                else {ans4=1;}
            }
            if(ans1+ans2+ans3+ans4<3){cout<<"NG"<<endl;}
            else{
                //不能拥有长度超过2(>=3)的字串重复
                bool book=false;
                for(int i=0;i+3<len;i++){
                    string str1=s.substr(i,3);
                    for(int j=i+1;j+3<len;j++){
                        string str2=s.substr(j,3);
                        if(str1==str2){
                            book=true;
                            break;
                        }
                    }
                }
                if(book) {cout<<"NG"<<endl;}
                else {cout<<"OK"<<endl;}
            }
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值