leetcode(14):字符串知识||多行字符串输入||HJ密码验证程序||1233leetcode||HJ字符统计

0.字符串基本概念只是

string.substr();
string.compare()
compare函数原型

int compare (const basic_string& s) const;
int compare (const Ch* p) const;
int compare (size_type pos, size_type n, const basic_string& s) const;
int compare (size_type pos, size_type n, const basic_string& s,size_type pos2, size_type n2) const;
int compare (size_type pos, size_type n, const Ch* p, size_type = npos) const;
  • compare函数使用例子
#include <iostream>
#include <string>
using namespace std;
int main ()
{
    string A ("aBcdef");
    string B ("AbcdEf");
    string C ("123456");
    string D ("123dfg");
    //下面是各种比较方法
    int m=A.compare (B); //完整的A和B的比较
    int p=A.compare(1,5,B,4,2); //"Bcdef"和"Ef"比较
    int q=C.compare(0,3,D,0,3); //"123"和"123"比较
    cout << "m = " << m << ", n = " << n <<", p = " << p << ", q = " << q << endl;
    cin.get();
    return 0;
}


1 多行字符串输入

1 多行字符串输入(C++)

getline在不同编译器上效果不同;
牛客网上多行字符串输入:

  • 1 方法
    vector<string> str;
    string temp;
    while(getline(cin,temp))
    {
        str.push_back(temp);
    }
  • 2 方法
	vector<string> str;
    string temp;
    while(cin>>temp)
    {
    //字符串处理函数
       str.push_back(temp);
    }
  • 3 方法 cin.peek()
#include <iostream>
using namespace std;
 
int main()
{
    int a, s=0;
    //回车表示数据输入结束
    while(cin.peek()!='\n')  //cin.peek()相当于偷看一眼再放回流中
    {
        cin>>a;
        s += a;
    }
    cout << s;
    return 0;
}

2 多行字符串输入(C语言)

#include <stdio.h>
int main()
{
    int a, s=0;
    //输入的第一个字符有可能是数字,所以用do while循环(即第一次循环体一定要执行)
    do
    {
        scanf("%d",&a);  // "%d",只有遇到数字scanf才会读入
        s += a;
    }while(getchar()!='\n');
    printf("%d",s);
    return 0;
}

2 HJ密码验证程序

题目要求:

在这里插入图片描述

主要涉及到3个知识点:多行输入(看第一张),检查至少三种类别字符,至少两个子串重复;

2.1 检查至少三种类别字符

c++或c#中要求引用 #include < cctype >

c中引用#include<ctype.h>

判断字母数字函数 isalnum()

判断字母函数 isalpha()

判断数字函数 isdigit()

判断空格 isspace()

判断标点符号 ispunct()

转换成小写 tolower()

转换成大写 toupper()

等,还有判断打印字符等

在这里插入图片描述
程序为:

bool Nom2Zi(string a)
{
    bool res = false;
    int tsg[5] = {0}; //小写 大写 数字 其他符号 总数
    for(int i=0;i<a.size();i++)
    {
        if(tsg[2] == 0 && isdigit(a[i]))
        {tsg[2]++;tsg[4]++;}
        else if(tsg[0] == 0 && islower(a[i]))
        {tsg[0]++;tsg[4]++;}
        else if(tsg[1] == 0 && isupper(a[i]))
        {tsg[1]++;tsg[4]++;}
        else if( tsg[3] == 0 && !isalnum(a[i]) ) //不是前面三种 
        {tsg[3]++;tsg[4]++;}
        
        if(tsg[4] >=3 )
        {res = true;
            break;}
            
    }
    
    return res;
}

2.2 子串重复长度至少为2

这个又更好的解法(以后更新),但是我使用的暴力搜索;
程序为:

bool isFCa(string a) //存在重复子串 true存在 false不存在
{
    bool res = false;
    int i=0;int j=0;
    for (int i = 0; i < a.size() - 5;i++){
        for (int j = i + 1; j < a.size() - 2 ; j++) //至少是3
        {
            if (a[i] == a[j] && a[i + 1] == a[j + 1] && a[i + 2] == a[j + 2])
            {
                res = true;
                break;
            }
        
    }
    }
    
    return res;
}

2.3 解题程序

#include <iostream>
#include <string>
#include <vector>
#include <cctype>
using namespace std;

bool Nom2Zi(string a)
{
    bool res = false;
    int tsg[5] = {0}; //小写 大写 数字 其他符号 总数
    for(int i=0;i<a.size();i++)
    {
        if(tsg[2] == 0 && isdigit(a[i]))
        {tsg[2]++;tsg[4]++;}
        else if(tsg[0] == 0 && islower(a[i]))
        {tsg[0]++;tsg[4]++;}
        else if(tsg[1] == 0 && isupper(a[i]))
        {tsg[1]++;tsg[4]++;}
        else if( tsg[3] == 0 && !isalnum(a[i]) ) //不是前面三种 
        {tsg[3]++;tsg[4]++;}
        
        if(tsg[4] >=3 )
        {res = true;
            break;}
            
    }
    
    return res;
}
bool isFCa(string a) //存在重复子串 true存在 false不存在
{
    bool res = false;
    int i=0;int j=0;
    for (int i = 0; i < a.size() - 5;i++){
        for (int j = i + 1; j < a.size() - 2 ; j++) //至少是3
        {
            if (a[i] == a[j] && a[i + 1] == a[j + 1] && a[i + 2] == a[j + 2])
            {
                res = true;
                break;
            }
        
    }
    }
    
    return res;
}

void process(string a)
{
    if(a.size()>8 && Nom2Zi(a) && !isFCa(a) )
        cout<<"OK"<<endl;
    else
        cout<<"NG"<<endl;
}

int main()
{
    
    vector<string> str;
    string temp;
    while(cin>>temp)
    {
        process(temp);
    }
    return 0;
    
    
}

3.HJ 字符统计

思路:map或者struct
利用vector进行自定义排序;
下面采用map和vector

#include <iostream>
#include <map>
#include <string>
#include <cctype>
#include <vector>
#include <algorithm>
using namespace std;

 bool cmp(const pair<char,int> &p1,const pair<char,int> &p2)
//要用常数,不然编译错误 
 {
     if(p1.second!=p2.second)
         return p1.second>p2.second;  //个数降序
     else
         return p1.first < p2.first; //字符升序
 }

int main()
{
    map<char,int> data;
    string str;
    getline(cin,str);
    map<char, int>::iterator iter;
    vector<int> index;
    for(int i=0;i<str.size();i++)
    {
        if(isalnum(str[i])||str[i] == ' ')
        {
            if(data.count(str[i])) //找到了
            {
                iter = data.find(str[i]);
                iter->second++;
            }
            else
                data.insert(make_pair(str[i], 1));
            //
            
        }
    }

    //for (map<char, int>::iterator iter = data.begin();iter!=)
     //   int res = data2.count(str[0]);
    vector< pair<char, int> > dd;
    for (map<char, int>::iterator iter = data.begin(); iter != data.end();iter++)
    {
        dd.push_back( make_pair ( iter->first, iter->second) );
    }

    sort(dd.begin(), dd.end(), cmp);

    for (vector<pair<char, int>>::iterator iter2 = dd.begin(); iter2 != dd.end();iter2++)
    {
        cout << iter2->first << "";
    }
    cout << endl;
}

//0751462839jcybodpnwxzikmfehluagtqrsv
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CoomCon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值