2552. 统计上升四元组

2552. 统计上升四元组


题目链接:2552. 统计上升四元组

代码如下:

//参考链接:https://leetcode.cn/problems/count-increasing-quadruplets/solutions/2080632/you-ji-qiao-de-mei-ju-yu-chu-li-pythonja-exja
class Solution
{
public:
	long long countQuadruplets(vector<int>& nums)
	{
		vector<vector<int>> great(nums.size(), vector<int>(nums.size() + 1));
		for (int k = nums.size() - 2; k >= 2; k--)
		{
			great[k] = great[k + 1];
			for (int x = 1; x < nums[k + 1]; x++)
			{
				great[k][x]++;
			}
		}

		long long res = 0;
		vector<int> less(nums.size() + 1);
		for (int j = 1; j < nums.size() - 2; j++)
		{
			for (int x = nums[j - 1] + 1; x <= nums.size(); x++)
			{
				less[x]++;
			}
			for (int k = j + 1; k < nums.size() - 1; k++)
			{
				if (nums[j] > nums[k])
				{
					res += less[nums[k]] * great[k][nums[j]];
				}
			}
		}
		return res;
	}
};
以下是输入文法四元组,判断是否为LL(1)文法的代码实现: ```python # 输入文法四元组 grammer = { 'S': ['aA', 'bB'], 'A': ['cA', 'd'], 'B': ['eB', 'f'] } terminals = ['a', 'b', 'c', 'd', 'e', 'f'] non_terminals = ['S', 'A', 'B'] # 构建FIRST集合 first = {} for symbol in terminals + non_terminals: first[symbol] = set() for terminal in terminals: first[terminal].add(terminal) for non_terminal in non_terminals: first[non_terminal] = set() for non_terminal in non_terminals: for production in grammer[non_terminal]: if production[0] in terminals: first[non_terminal].add(production[0]) else: first[non_terminal] |= first[production[0]] # 构建FOLLOW集合 follow = {} for non_terminal in non_terminals: follow[non_terminal] = set() follow['S'].add('$') while True: updated = False for non_terminal in non_terminals: for production in grammer[non_terminal]: for i in range(len(production)): if production[i] in non_terminals: if i == len(production) - 1: if follow[non_terminal] != follow[production[i]]: follow[production[i]] |= follow[non_terminal] updated = True else: first_set = first[production[i+1]].copy() if '' in first_set: first_set.remove('') if follow[non_terminal] != follow[production[i]] | first_set: follow[production[i]] |= follow[non_terminal] | first_set updated = True else: if follow[non_terminal] != first_set: follow[production[i]] |= first_set updated = True if not updated: break # 构建LL(1)预测分析表 table = {} for non_terminal in non_terminals: table[non_terminal] = {} for terminal in terminals: table[non_terminal][terminal] = [] for non_terminal in non_terminals: for production in grammer[non_terminal]: first_set = set() for i in range(len(production)): if production[i] in terminals: first_set = {production[i]} break else: first_set |= first[production[i]] if '' not in first[production[i]]: break for terminal in first_set: table[non_terminal][terminal].append(production) if '' in first_set: for terminal in follow[non_terminal]: table[non_terminal][terminal].append(production) # 判断文法是否为LL(1)文法 is_ll1_grammer = True for non_terminal in non_terminals: for terminal in terminals: if len(table[non_terminal][terminal]) > 1: is_ll1_grammer = False print(f"({non_terminal}, {terminal}): {table[non_terminal][terminal]}") print("该文法是LL(1)文法" if is_ll1_grammer else "该文法不是LL(1)文法") ``` 上述代码构建了文法的FIRST集合、FOLLOW集合和LL(1)预测分析表,并通过预测分析表判断该文法是否为LL(1)文法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值