密码验证合格程序
2016-04-05 15:50
351 查看
题目描述
密码要求:1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有相同长度超2的子串重复
说明:长度超过2的子串
[b]输入描述:[/b]
一组或多组长度超过2的子符串。每组占一行
[b]输出描述:[/b]
如果符合要求输出:OK,否则输出NG
[b]输入例子:[/b]
021Abc9000 021Abc9Abc1 021ABC9000 021$bc9000
[b]输出例子:[/b]
OK NG NG OK 思路1:题目有三个要求,一是密码长度要大于8;二是至少包括三种符号;三是长度超过2的子字符串不能有重复。前两个条件好写,主要是判断重复子字符串,这里比较简单的判断方法是从第一个字符开始连续截取长度为三的子字符串,然后判断后面的子字符串是否包含此长度为三的字符串,若包含,则为NG。理由是即便字符串中有长度大于三的重复的字符串,它同时也包含了长度为三的子字符串。
public static boolean judge(String str) { int length = str.length(); for (int i = 0; i < length; i++) { for (int j = i + 3; j < length; j++) { String substr1 = str.substring(i, j); String substr2 = str.substring(j); if (substr2.contains(substr1)) { return false; } } } return true; }
思路2:也是比较常规的想法,从第一个字符开始,分别与间隔大于等于3的字符比较,若相等,则比较第二个字符,若再相等,继续比较第三个字符...若其中只有一个或者两个相等,则进行还原—还是从第一个字符比较,间隔大于3的字符为还没有与第一个字符比较的字符,描述不太清楚,看代码,参考了网友的:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { char[] c = in.nextLine().toCharArray(); if (c.length <= 8 || !judge1(c) || !judge2(c)) { System.out.println("NG"); } else System.out.println("OK"); } } public static boolean judge2(char[] c) { int start1 = 0;//索引为start1的字符分别与间隔>=3的字符比较 int start2 = start1+3;//间隔>=3的字符,首次为 int count = 0; for (int i = 0; i < c.length; i++) { start1 = i; start2 = start1 + 3; count = 0; while (start1 < start2 && start2 < c.length) { if (c[start1] == c[start2]) { count++; if (count >= 3) return false; start1++; start2++; } else if (start1 != i) {//若上面if语句执行但方法但还不满足count>=3;此处还原start1,start2为还没与start1比较的第一个字符 // if (count == 1) { // count = 0; // start1 = i; // } else if (count == 2) { // count = 0; // start1 = i; // start2 -= 1; // } start1=i;//上面两个判断的通用写法 start2=start2-(count-1); count=0; } else { count = 0; start2++; } } } return true; } public static boolean judge1(char[] c) { int digit = 0; int Lowercase = 0; int Uppercase = 0; int other = 0; for (int i = 0; i < c.length; i++) { if ('0' <= c[i] && c[i] <= '9') { digit = 1; } else if ('a' <= c[i] && c[i] <= 'z') { Lowercase = 1; } else if ('A' <= c[i] && c[i] <= 'Z') { Uppercase = 1; } else { other = 1; } } return (digit + Lowercase + Uppercase + other) >= 3; } }
相关文章推荐
- LC30 Substring with Concatenation of All Words
- django之异常错误3(Student matching query does not exist.)
- Google自己的下拉刷新组件 SwipeRefreshLayout + RecyclerView 实现 上拉刷新 和 下拉刷新
- HDU 4289 最大流最小割
- asp.net mvc_html.ActionLink的几种参数格式
- Android日志处理相关
- 每天学点Excel技巧-Day4
- Spring Mvc问题之session存取出现错误问题
- UI设计理念-app的界面设计流程
- [java-solr日常一]solr4.9同步mysql数据
- 为Docker创建自动化nginx反向代理
- Sql发布订阅设置不初始化订阅库架构的设置
- android5.0 activity动画
- 记录博客的开通
- CentOS 6 升级MySql (5.1.x 到 5.5, 再到5.6)
- 使用React实现类似快递单号查询效果
- iTween基础之Color(变换颜色)
- c++基础复习:c++模板编程常用用法整理
- MyBatis中的批量删除操作
- iOS--项目目录组织及生成脚本