C++ 哈希算法、贪心算法

哈希算法:辅助数组h[1001], h[x]代表x出现的次数,整数技术器c。

算法:遍历这n个数,对于遍历到的第i个数x,计算出以下数字 y = h[x - 1] + h[x + 1]直接累加到计数器c上(因为h[x]代表x的出现次数),把当前遍历到的x这个数,执行以下操作,h[x] = h[x] + 1,代表增加x的计数。(缺点:如果数字范围很大很散,就会导致开不出这么大的空间)。

为了解决上述提到的缺点,会采取取模等方式,也会出现哈希冲突等情况,也会有对应的哈希冲突解决方案。

代码分析:

1 初始化哈希表

function hashInit(){

for i -> (0, HMAX-1)

hash[i] = -1;

}

2 哈希插入

function hashInsert(x){

y = x % HMAX;

while(1)

if(hash[y] == -1)

hash[y] = x

cnt[y] = 1

return

else if(hash[y] == x)

cnt[y]++

return

if(++y >= HMAX)

y = 0

}

哈希查找

function hashGet(x)

y = x % HMAX

while(1)

if(hash[y] == -1)

return 0

else if(hash[y] == x)

return cnt[y]

if(++y >= HMAX)

y = 0

在C++中,unordered_map 是对应的哈希表

代码练习,对应蓝桥云课 字符统计 代码见下

cpp 复制代码
#include <iostream>
#include <string>

using namespace std;
int main()
{
  string s;
  int hash[256] = {0};
  cin >> s;
  for(int i=0; i<s.size(); ++i){
    hash[s[i]]++;
  }

  int maxc = 0;
  char ret[256];
  int retSize = 0;
  for(char c = 'A'; c <= 'Z'; c++){
    if(hash[c] > maxc){
      maxc = hash[c];
      retSize = 0;
      ret[retSize++] = c;
    }else if(hash[c] == maxc){
      ret[retSize++] = c;
    }


  }
  ret[retSize] = '\0';
  cout << ret << endl;
  // 请在此输入您的代码
  return 0;
}

代码练习 2 对应蓝桥云课 字符串统计,代码见下

cpp 复制代码
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main()
{
  unordered_map<string, int> cnt;
  int n;
  cin >> n;
  for(int i=0; i<n; ++i){
    string s;
    cin >> s;
    cnt[s] = 1;
  }
  cout << cnt.size() << endl;

  // 请在此输入您的代码
  return 0;
}

代码练习三,对应蓝桥,优质数对,代码见下

cpp 复制代码
#include <iostream>
#include <unordered_map>
using namespace std;

#define SB 1000000001
#define ll long long
int n;
int A[100001];
int B[100001];

int main()
{
  cin >> n;
  for(int i=0; i<n; ++i){
    cin >> A[i];
  }
    for(int i=0; i<n; ++i){
    cin >> B[i];
  }
  unordered_map<ll, int> h;
  long long ret = 0;
  for(int j=0; j<n; ++j){
    ll y = (ll)B[j]*SB + A[j];
    ret += h[y];
    ll x = (ll)A[j]*SB + B[j];
    h[x]++;
  }
  cout << ret << endl;

  // 请在此输入您的代码
  return 0;
}

贪心算法在每一步均采取当前状态下的最优(局部最优)策略,以期望获得全局最优解

代码练习 对应蓝桥云课 翻硬币,代码见下

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;

string s, t;

int main()
{
  cin >> s;
  cin >> t;
  int n = s.size();
  int ret = 0;
  for(int i=0; i<n; ++i){
    if(s[i] != t[i]){
      s[i] = (s[i] == 'o' ? '*' : 'o');
      s[i+1] = (s[i+1] == 'o' ? '*' : 'o');
      ++ret;
    }
  }
  cout << ret << endl;
  // 请在此输入您的代码
  return 0;
}

代码练习,对应蓝桥云课 一键3连,代码见下

cpp 复制代码
#include <iostream>
#include <algorithm>
using namespace std;

int n;
int A[100001];

int main()
{
  cin >> n;
  for(int i=0; i<n; ++i){
    cin >> A[i];
  }
  sort(A, A+n);
  int l=1, r=1;
  while(r < n){
    if(A[r] != A[l-1]){
      A[l++] = A[r];
    }
    r++;
  }
  int res = 0;
  for(int i=0; i+2<l; ++i){
    int a = A[i];
    if(A[i+1] == a+1 && A[i+2] == a+2){
      ++res;
    }
  }
  cout << res << endl;
  // 请在此输入您的代码
  return 0;
}

代码练习,对应蓝桥云课 分开元音字母,代码见下

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;

bool isYuanyin(char c){
  return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}

string s;
string ret;

int main()
{
  cin >> s;
  ret = "(";
  bool flag = false;
  int cnt = 0;
  for(int i=0; i<s.size(); ++i){
    if(isYuanyin(s[i])){
      cnt++;
      flag = true;
      if(cnt > 1){
        ret += ")";
        ret += "(";
        cnt = 1;
      }
    }
    ret += s[i];
  }
  ret += ")";
  if(flag == false){
    ret = s;
  }
  // 请在此输入您的代码
  cout << ret;
  return 0;
}
相关推荐
anlogic几秒前
Java基础 8.18
java·开发语言
沐知全栈开发36 分钟前
WebForms XML 文件详解
开发语言
scx201310041 小时前
20250814 最小生成树和重构树总结
c++·算法·最小生成树·重构树
阿巴~阿巴~1 小时前
冒泡排序算法
c语言·开发语言·算法·排序算法
看到我,请让我去学习2 小时前
QT - QT开发进阶合集
开发语言·qt
weixin_307779132 小时前
VS Code配置MinGW64编译SQLite3库
开发语言·数据库·c++·vscode·算法
励志不掉头发的内向程序员4 小时前
STL库——string(类函数学习)
开发语言·c++
一百天成为python专家4 小时前
Python循环语句 从入门到精通
开发语言·人工智能·python·opencv·支持向量机·计算机视觉
Sunhen_Qiletian4 小时前
朝花夕拾(五)--------Python 中函数、库及接口的详解
开发语言·python
hqwest4 小时前
C#WPF实战出真汁07--【系统设置】--菜品类型设置
开发语言·c#·wpf·grid设计·stackpanel布局