题目
密码要求:
1.长度超过8位
2.包括大小写字母.数字.其它符号,以上四种至少三种
3.不能有相同长度大于等于2的子串重复
输入描述:
一组或多组长度超过2的子符串。每组占一行
输出描述:
如果符合要求输出:OK,否则输出NG
思路
分别处理三个要求,难点在于查找查找长度大于2的重复字符串,代码中有具体的注释
代码
#include<iostream>
#include<string>
#include<vector>
#include<cctype>
using namespace std;
int main()
{
string str;
while(cin >> str){
vector<int> flag(4);//flag数组,标记密码中是否有该数据类型(题目中总共有四种数据类型)
int count=0;
if(str.length() <= 8){
goto NG;
}
//处理第二个要求,包括三种类型的符号
else {
for(int i=0;i<str.length();++i){
if(isdigit(str[i]) && flag[0] == 0)
flag[0] = 1;
else if(islower(str[i]) && flag[1] == 0)
flag[1] = 1;
else if(isupper(str[i]) && flag[2] == 0)
flag[2] = 1;
else if(!isalnum(str[i]) && flag[3] == 0)
flag[3] =1;
}
for(int i = 0;i<4;++i)
{
if(flag[i] == 1)
++count;
}
if(count < 3){
goto NG;
}
//处理第三个要求,不能有长度大于等于2的字串重复
else{
//处理第三个要求,不能有长度大于等于2的字串重复
for(int i = 0; i <= str.size()-6; i++){
for(int j = i+3;j < str.size();j++){
if(str[i] == str[j] && str[i+1] == str[j+1] &&str[i+2] == str[j+2])
goto NG;
}
}
}
}
OK:
cout<<"OK"<<endl;continue;
NG:
cout<<"NG"<<endl;
}
return 0;
}
问题
剑指 Offer 48. 最长不含重复字符的子字符串
请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度。
思路
滑动窗口,应用set保证当前窗口内的元素唯一
代码
class Solution {
public:
int lengthOfLongestSubstring(string s) {
set<char> Set;
int b=0,e=0,maxlen=0;
while(e<s.length()){
while(Set.count(s[e]) !=0){
Set.erase(s[b++]);
}
Set.insert(s[e++]);
maxlen = max(maxlen,e - b);
}
return maxlen;
}
};
改进:用数组作hash,效率更高:
class Solution
{
public:
int lengthOfLongestSubstring(string s)
{
int fast = 0, slow = 0;
bool flag[256] = {0};//用一个flag数组,来标识窗口字符唯一
int ret = 0;
while (fast < s.size()) {
//满足条件的话,fast++扩大窗口
while (fast < s.size() && !flag[s[fast]]) {
flag[s[fast]] = true;
fast++;
}
//计算最大长度的值
ret = max(ret, fast - slow);
//不满足条件的话,slow++缩减窗口
flag[s[slow]] = 0;
slow++;
}
return ret;
}
};