CMB5. 卡中心密码安全规范
描述
小明在卡中心工作,用到的很多系统账号都需要设置安全密码。密码如果符合以下规范可以称为安全密码:输入描述
输入为一个字符串作为密码。输出描述
输出为将该密码改为安全密码的最小改变次数。如果它已经是安全密码,则返回0。示例1
输入:
aB3ab3
输出:
0
示例2
输入:
aaaaaa
输出:
2
示例3
输入:
abcdefG
输出:
1
C 解法, 执行用时: 2ms, 内存消耗: 364KB, 提交时间: 2019-03-30
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> int main() { char C[100], S[21]; gets(C); int low_c = 0, up_c = 0, num = 0, flag = 1; int i, n, count = 0, n1 = 0, j = 0, n2 = 0; if(strlen(C) > 20) { for(i = 0; i < strlen(C); i++) { if(C[i] == C[i + 1] && C[i + 1] == C[i + 2]) { if(strlen(C) - n1 == 20) { S[j] = C[i]; j ++; } else { n1 ++; S[j] = C[i]; S[j + 1] = C[i + 1]; j = j + 2; i = i + 2; } } else { S[j] = C[i]; j ++; } } //printf("%d\n", j); } else strcpy(S, C); if(strlen(C) - n1 > 20) { strncpy(S, C, 20); n2 = strlen(C) - n1 - 21; } //printf("%d\n", n2); for(i = 0; i < strlen(C) - n1; i++) { if(S[i] >= 'a' && S[i] <= 'z') { low_c = 1; } if(S[i] >= 'A' && S[i] <= 'Z') { up_c = 1; } if(S[i] >= '0' && S[i] <= '9') { num = 1; } if(S[i] == S[i + 1] && S[i + 1] == S[i + 2]) { i = i + 2; flag = 0; count ++; } } if(flag == 0) { printf("%d", count + n1 + n2); return 0; } n = 3 - low_c - up_c - num; printf("%d", n + n1 + n2); }
C 解法, 执行用时: 2ms, 内存消耗: 376KB, 提交时间: 2020-12-23
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> int main() { char C[100], S[21]; gets(C); int low_c = 0, up_c = 0, num = 0, flag = 1; int i, n, count = 0, n1 = 0, j = 0, n2 = 0; if(strlen(C) > 20) { for(i = 0; i < strlen(C); i++) { if(C[i] == C[i + 1] && C[i + 1] == C[i + 2]) { if(strlen(C) - n1 == 20) { S[j] = C[i]; j ++; } else { n1 ++; S[j] = C[i]; S[j + 1] = C[i + 1]; j = j + 2; i = i + 2; } } else { S[j] = C[i]; j ++; } } } else strcpy(S, C); if(strlen(C) - n1 > 20) { strncpy(S, C, 20); n2 = strlen(C) - n1 - 21; } for(i = 0; i < strlen(C) - n1; i++) { if(S[i] >= 'a' && S[i] <= 'z') { low_c = 1; } if(S[i] >= 'A' && S[i] <= 'Z') { up_c = 1; } if(S[i] >= '0' && S[i] <= '9') { num = 1; } if(S[i] == S[i + 1] && S[i + 1] == S[i + 2]) { i = i + 2; flag = 0; count ++; } } if(flag == 0) { printf("%d", count + n1 + n2); return 0; } n = 3 - low_c - up_c - num; printf("%d", n + n1 + n2); }
C 解法, 执行用时: 2ms, 内存消耗: 376KB, 提交时间: 2019-08-19
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> int main() { char C[100], S[21]; gets(C); int low_c = 0, up_c = 0, num = 0, flag = 1; int i, n, count = 0, n1 = 0, j = 0, n2 = 0; if(strlen(C) > 20) { for(i = 0; i < strlen(C); i++) { if(C[i] == C[i + 1] && C[i + 1] == C[i + 2]) { if(strlen(C) - n1 == 20) { S[j] = C[i]; j ++; } else { n1 ++; S[j] = C[i]; S[j + 1] = C[i + 1]; j = j + 2; i = i + 2; } } else { S[j] = C[i]; j ++; } } } else strcpy(S, C); if(strlen(C) - n1 > 20) { strncpy(S, C, 20); n2 = strlen(C) - n1 - 21; } for(i = 0; i < strlen(C) - n1; i++) { if(S[i] >= 'a' && S[i] <= 'z') { low_c = 1; } if(S[i] >= 'A' && S[i] <= 'Z') { up_c = 1; } if(S[i] >= '0' && S[i] <= '9') { num = 1; } if(S[i] == S[i + 1] && S[i + 1] == S[i + 2]) { i = i + 2; flag = 0; count ++; } } if(flag == 0) { printf("%d", count + n1 + n2); return 0; } n = 3 - low_c - up_c - num; printf("%d", n + n1 + n2); }
C++14 解法, 执行用时: 2ms, 内存消耗: 380KB, 提交时间: 2020-08-11
#include <iostream> #include <string> using namespace std; int main(){ string s; cin >> s; int len = s.size(); int lower = 0, upper = 0, num = 0; int cnt = 0, rep = 0; int res = 0; for(int i = 0; i < s.size(); ++i){ if(cnt > 0 && s[i] == s[i-1]){ ++cnt; if(cnt == 3){ ++res; if(len > 20){//同时减少达到3个连续字符处的重复字符(最划算,同时解决1、3) --len; cnt = 2; } else{ cnt = 0; ++rep;//多少组达到3的连续字符 } } } else cnt = 1; if(islower(s[i])) lower = 1; else if(isupper(s[i])) upper = 1; else if(s[i] >= '0' && s[i] <= '9') num = 1; } int f =3-lower-upper-num; if(rep >= f) f = 0;//需要以插入形式得到的之前没有的大写/小写/数字(不超过rep的部分可以通过替换得到,同时解决2、3) else f -= rep; if(len < 6){ res += std::max(6-len, f); } else if(len > 20){ res += len - 20;//删除 res += f;//插入 } else res += f; cout<<res<<endl; return 0; }
C++ 解法, 执行用时: 2ms, 内存消耗: 408KB, 提交时间: 2020-08-19
#include <iostream> #include<bits/stdc++.h> using namespace std; int main() { int len,i,j,ans,res3,res2,res1,res4,res5,res6; bool n,d,x; char s[1000]; gets(s); len=strlen(s); n=d=x=0; res3=res4=res5=res6=0; j=-1; for(i=0;i<len;i++) { if('A'<=s[i]&&s[i]<='Z'&&d==0) { d=1; } if('a'<=s[i]&&s[i]<='z'&&x==0) { x=1; } if('0'<=s[i]&&s[i]<='9'&&n==0) { n=1; } if(i>=2) { if(s[i]==s[i-1]&&s[i]==s[i-2]&&s[i]==s[i+1]&&s[i]==s[i+2]&&i<len-2) { if(i-j>=3) { res6++; } } if(s[i]==s[i-1]&&s[i]==s[i-2]&&s[i]==s[i+1]&&i<len-1) { if(i-j>=3) { res5++; } } if(s[i]==s[i-1]&&s[i]==s[i-2]) { if(i-j>=3) { res3++; j=i; } res4++; } } } res2=0; res3=res3-res5; res5=res5-res6; if(d==0) res2++; if(x==0) res2++; if(n==0) res2++; if(len<6) { res1=6-len; ans=max(res1,res3+res5+res6); ans=max(ans,res2); } else { if(len>20) { res1=len-20; //printf("%d %d\n",res1,res4); if(res1>=res4) { ans=res1+res2; } else { if(res3>=res1) { if(res2<=(res3+res5+res6-res1)) { ans=res3+res5+res6; } else { ans=res1+res2; } } else { ans=res3; res1=res1-res3; for(;res1>0;) { if(res5>0&&res1>=2) { res1=res1-2; res5--; ans=ans+2; } if(res1==1) { ans++; res1=0; } if(res1==2&&res5==0) { ans=ans+2; res1=0; } if(res6>0&&res1>=3) { res1=res1-3; res6--; ans=ans+3; } } if(res2<=res6+res5) { ans=ans+res6+res5; } else { ans=ans+res2; } } } } else { res1=0; ans=max(res1,res3+res5+res6); ans=max(ans,res2); } } printf("%d\n",ans); return 0; }