【算法作业】次数不小于k的字符,n/2的选票

Uncategorized
1.9k words

问题描述

  1. 给定一个字符串str和一个整数K,设计策略返回一个最大子串长度使得子串每个字符重复出现的次数不小于K。

  2. 假设一个由n人组成的小组中的每个人都从一组候选人中选出两个人来填补委员会的两个职位。排名前两位的选手只要各自获得超过n/2的选票,都会赢得位置。设计算法,确定获得最多选票的两位候选人是否各自获得了至少n/2张选票,如果是,则确定这两位候选人是谁。

最近的题目真是越来越难了(T^T)

解题思路

T1

  1. 方法一:

一般在求区间什么最大最小的(比如求区间最大和),根据经验能采用前缀和的思路。

假设字符串只有26个小写英文字母。我们定义sum[i][j]表示str[0 , i-1]中j代表的字符出现的次数。那么子串str[l,r]中j代表的字符出现的次数即为sum[r+1][j]-sum[l][j]。只需要预先处理出所有sum,就能O($ n^2 $)处理出子串中每个字符出现次数

  1. 方法二:

第一种方法简单易想,现在着重讲解第二种方法(滑动窗口法)

让我们逐步解释这个算法的工作原理:

  1. 外部循环

    • 外部循环控制着不同字符个数的范围,从 1 到 26。这意味着我们会检查子串中包含的不同字符的个数,从只有一个字符到所有 26 个字符都出现。
  2. 内部滑动窗口

    • 内部有一个滑动窗口,由两个指针 l 和 r 控制。初始时,l 和 r 都指向字符串的起始位置。
    • 每次迭代,r 指针向右移动,扩展窗口,并根据窗口内的字符更新 mp 数组和 sumcnt 变量:
      • mp 数组记录了窗口内每个字符出现的次数。
      • sum 变量表示窗口内所有字符出现次数的总和。
      • cnt 变量表示窗口内不同字符的个数。
    • 在更新 mp 数组和变量后,检查是否满足了每个字符至少重复 k 次的条件。如果是,则更新答案。
    • 如果窗口内的不同字符个数超过了当前循环的限制(即 cnt > i),则收缩窗口,移动左指针 l,并更新 mp 数组和变量。
    • 在窗口内的字符满足条件时,更新最大子串的长度。
  3. 返回结果

    • 最后,函数返回找到的最大子串长度。

这种方法的优点是它只需遍历字符串一次,并且在每次迭代中,窗口的大小是可变的,因此在满足条件的情况下,它能找到最大的子串长度,时间复杂度为O(n)。

T2

  1. 方法一:

拿到这个题目直观地能想到用一个哈希表记录每个人的得票数。

最后遍历一遍哈希表找到最大和次大值判断是否大于n/2。

代码很简单,这里就不放了。

性能分析:
时间复杂度:O(n) 空间复杂度:O(n)

  1. 方法二:

Boyer-Moore投票算法

  1. findMajorityCandidates 函数接受一个整数数组 votes、数组长度 n,以及两个引用参数 candidate1candidate2

  2. 第一阶段:

    • 在第一个阶段中,使用Boyer-Moore投票算法来确定获得最多选票的两位候选人。
    • 遍历投票数组,对每一个候选人进行投票统计。
    • 如果当前候选人是已知的 candidate1candidate2,则增加其对应的票数。
    • 如果当前候选人不是已知的 candidate1candidate2,并且 count1 为 0,则将当前候选人设为 candidate1,并将票数 count1 设为 1。
    • 如果当前候选人不是已知的 candidate1candidate2,并且 count2 为 0,则将当前候选人设为 candidate2,并将票数 count2 设为 1。
    • 如果当前候选人不是已知的 candidate1candidate2,并且 count1count2 都不为 0,则将 count1count2 各自减 1。
  3. 第二阶段:

    • 在第二阶段中,对获得最多选票的两位候选人进行验证,判断他们是否各自获得了超过 n/2 的选票。
    • 重新统计投票数组中出现的 candidate1candidate2 的次数。

使用Boyer-Moore投票算法来高效地找出获得最多选票的两位候选人,并且在第二阶段对他们的选票数进行验证,保证他们各自获得了超过 n/2 的选票。

完整代码

T1

  1. 方法一:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<iostream>
#include<string>
using namespace std;
int max(int a,int b){
if(a>b)
return a;
else
return b;
}
int main(){
string str = "abaabcd";
int k=2,len,ans=0;
int sum[1000][26] = {0};
len = str.length();
sum[0][str[0]-'a']++;
for(int i=1;i<len;i++){
for(int j=0;j<26;j++)
sum[i][j]=sum[i-1][j];
sum[i][str[i]-'a']++;
}
for(int i=0;i<len;i++){
for(int j=i;j<len;j++){
bool ok=true;
for(int k=0;k<26;k++){
int cnt = sum[j][k] - sum[i][k];
if(cnt>0 && cnt<k){
ok=false;
break;
}
}
if(ok)
ans = max(ans, j-i+1);
}
}
cout<<ans<<endl;

for(int i=0;i<len;i++){
for(int j=0;j<26;j++)
cout<<sum[i][j]<<" ";
cout<<endl;
}

return 0;
}

运行结果

1
2
3
4
5
6
7
8
5
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3 2 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  1. 方法二:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int longestSubstring(string s, int k) {
int n = s.length();
int ans = 0;

for(int i = 1; i <= 26; ++i) {
int l = 0, r = -1;
int mp[26] = {0}, sum = 0, cnt = 0;

while(l < n) {
while(cnt <= i && r < n - 1) {
++r;
if(mp[s[r] - 'a'] == 0) ++cnt, ++sum;
if(mp[s[r] - 'a'] == k - 1) --sum;
mp[s[r] - 'a']++;
}
if(cnt > i) {
if(mp[s[r] - 'a'] == 1) --cnt, --sum;
if(mp[s[r] - 'a'] == k) ++sum;
mp[s[r] - 'a']--;
--r;
}
if(sum == 0) ans = max(ans, r - l + 1);
if(mp[s[l] - 'a'] == 1) --cnt, --sum;
if(mp[s[l] - 'a'] == k) ++sum;
mp[s[l] - 'a']--;
++l;
}
}
return ans;
}

int main() {
string s = "abaabcd";
int k = 2;
cout << "最大子串长度: " << longestSubstring(s, k) << endl;
return 0;
}

T2

方法二:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>

using namespace std;

void findMajorityCandidates(int votes[], int n, int& candidate1, int& candidate2) {
candidate1 = -1;
candidate2 = -1;
int count1 = 0, count2 = 0;

// 第一阶段:投票
for (int i = 0; i < n; ++i) {
if (candidate1 == votes[i]) {
count1++;
} else if (candidate2 == votes[i]) {
count2++;
} else if (count1 == 0) {
candidate1 = votes[i];
count1 = 1;
} else if (count2 == 0) {
candidate2 = votes[i];
count2 = 1;
} else {
count1--;
count2--;
}
}

// 第二阶段:验证候选人是否获得超过n/2的选票
count1 = count2 = 0;
for (int i = 0; i < n; ++i) {
if (candidate1 == votes[i]) {
count1++;
} else if (candidate2 == votes[i]) {
count2++;
}
}
}

int main() {
int votes[] = {1, 2, 2, 2, 1, 2, 2, 2, 3, 3, 3, 3, 3};
int n = sizeof(votes) / sizeof(votes[0]);
int candidate1, candidate2;

findMajorityCandidates(votes, n, candidate1, candidate2);

if (candidate1 != -1 && candidate2 != -1) {
cout << "获得最多选票的两位候选人分别是:" << candidate1 << " 和 " << candidate2 << endl;
} else {
cout << "没有两位候选人各自获得超过n/2的选票" << endl;
}

return 0;
}

运行结果:

1
获得最多选票的两位候选人分别是:3 和 2