UVA 11235 - Frequent values (RMQ的基础应用)

题意:给出一个非降序排列的整数数组a[1], a[2], ...... , a[n],给出一系列询问(i, j),回答a[i], a[i+1], ...... , a[j]中出现最多的值所出现的次数。

思路:典型的RMQ应用,第一次仿着写,将数组游程编码,value[i]和cnt[i]分别表示第i段的数值和出现次数,num[p], left[p], right[p]分别表示位置p所在段的编号和左右端点的位置。则查询(L, R)的结果为以下三部分的最大值:从L到L所在段的结束的个数(right[L]-L+1),从R所在段的开始到R处的个数(R-left[R]+1),中间从num[L]+1到num[R]-1段的count的最大值。如果L和R在同一段,则答案就是R-L+1。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXN = 100010;

int dp[MAXN][30],value[MAXN],cnt[MAXN],num[MAXN],left[MAXN],right[MAXN];

int RMQ(int l,int r){
    if (r < l)
        return 0;
    int k = log(1.0*r-l+1) / log(2.0);
    return max(dp[l][k],dp[r-(1<<k)+1][k]);
}

int main(){
    int x,n,m,q;
    while (scanf("%d",&n) != EOF && n){
        scanf("%d",&q);
        memset(right,0,sizeof(right));
        m = 0;
        for (int i = 1; i <= n; i++){
            scanf("%d",&x);
            if (!m || value[m] != x){
                value[++m] = x;
                cnt[m] = 1;
            }
            else cnt[m]++;
            num[i] = m;
        }
        cnt[m+1] = n;
        int k = 1,temleft = 1,temright = cnt[1];
        for (int i = 1; i <= m; i++){
            for (int j = 1; j <= cnt[i]; j++){
                left[k] = temleft;
                right[k++] = temright;
            }
            temleft += cnt[i];
            temright += cnt[i+1];
        }
        for (int i = 1; i <= m; i++)
            dp[i][0] = cnt[i]; // 第i段
        for (int j = 1; (1<<j) <= m; j++)
            for (int i = 1; i + (1<<j)-1 <= m; i++)
                dp[i][j] = max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
        int l,r;
        while (q--){
            scanf("%d %d",&l,&r);
            if (left[l] == left[r])
                printf("%d\n",r-l+1);
            else printf("%d\n",max(max(right[l]-l+1,r-left[r]+1),RMQ(num[l]+1,num[r]-1)));
        }
    }
    return 0;
}


FP-growth算法是一种用于频繁模式挖掘的算法,常用于数据挖掘、市场分析和推荐系统等领域。下面是使用Python实现FP-growth算法的代码: 首先,需要导入相应的库: ``` from collections import defaultdict from itertools import chain from typing import List, Tuple ``` 接着,定义一些常量: ``` # 定义FP树节点 class FPTreeNode: def __init__(self, item=None, count=1, parent=None): self.item = item self.count = count self.parent = parent self.children = defaultdict(FPTreeNode) # 定义FP树 class FPTree: def __init__(self, transactions, support, root_value, root_count): self.frequent_items = self.find_frequent_items(transactions, support) self.headers = self.build_header_table(self.frequent_items) self.root = self.build_fptree(transactions, root_value, root_count, self.frequent_items, self.headers) # 定义FP-growth算法 class FPGrowth: def __init__(self, min_support=0.5, min_confidence=0.5): self.min_support = min_support self.min_confidence = min_confidence # 定义函数:寻找频繁项集 def find_frequent_items(self, transactions, support): items = defaultdict(lambda: 0) for transaction in transactions: for item in transaction: items[item] += 1 # 去除不符合最小支持度的项 items = dict((item, support) for item, support in items.items() if support >= support * len(transactions)) # 返回频繁项集 return items ``` 接着,实现构建FP树的函数: ``` # 定义函数:构建FP树 def build_fptree(self, transactions, root_value, root_count, frequent_items, headers): root = FPTreeNode(item=root_value, count=root_count) for transaction in transactions: sorted_items = sorted([item for item in transaction if item in frequent_items], key=lambda item: frequent_items[item], reverse=True) if len(sorted_items) > 0: self.insert_tree(sorted_items, root, headers) return root # 定义函数:插入节点到FP树中 def insert_tree(self, items, node, headers): if items[0] in node.children: child = node.children[items[0]] else: child = FPTreeNode(item=items[0], parent=node) headers[items[0]].append(child) node.children[items[0]] = child if len(items) > 1: self.insert_tree(items[1:], child, headers) child.count += 1 ``` 最后,实现FP-growth算法的主函数: ``` # 定义函数:寻找频繁模式 def find_frequent_patterns(self, transactions): if not transactions: return None # 构建FP树 support = self.min_support root_value = 'null' root_count = len(transactions) fp_tree = FPTree(transactions, support, root_value, root_count) # 寻找频繁项集和条件模式基 frequent_patterns = defaultdict(int) conditional_patterns = defaultdict(list) self.mine_patterns(fp_tree, fp_tree.header_table, frequent_patterns, conditional_patterns) # 返回频繁模式 return frequent_patterns # 定义函数:挖掘频繁项集 def mine_patterns(self, tree, headers, frequent_patterns, conditional_patterns): sorted_items = [item[0] for item in sorted(headers.items(), key=lambda x: x[1][0].count)] for item in sorted_items: base_patterns = [path(item_node) for item_node in headers[item]] frequent_patterns.update({tuple(pattern): headers[item][0].count for pattern in base_patterns}) conditional_tree = self.build_conditional_tree(base_patterns, headers[item]) if conditional_tree: self.mine_patterns(conditional_tree, conditional_tree.header_table, frequent_patterns, conditional_patterns) ``` 这样,我们就完成了使用Python实现FP-growth算法的代码。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值