文章目录
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