P7623 [AHOI2021初中组] 收衣服-递推DP

[AHOI2021初中组] 收衣服 - 洛谷

本题我们后推前

设推到位置i,那么【1..i-1]其实已经按顺序排列好了。由于是全排列,我们本次要寻找的数字i,可能的位置有n-i种,我们枚举这n-i+1种位置,对于每一个位置,剩下的数字也是全排列,所以每一个位置就代表了  factor[n-i] 种情况,一旦我们对某个位置这样做了,那么我们排序代价之和就是w[i][j]*factor[n-i]。但是,这仅仅是这一个位置,这一个数的排序代价。每一个位置排序之后,还需要剩下的n-i个数进行排序,故考虑承接前n-i次,即对每一个i每一个位置归位后,还必须都加上dp[i+1],表示承接了将后面数字归位的代价

#include<iostream>
#include<cstring>
#include<cstdio>
# define mod 998244353
using namespace std;
typedef long long int ll;
ll factor[550];
ll dp[550];
ll w[550][550];

int main()
{


  factor[1]=1;

  for(int i=2;i<=500;i++)
  {
      factor[i]=(i*factor[i-1])%mod;
  }

  int n;

  cin>>n;


  for(int i=1;i<n;i++)
  {
      for(int j=i;j<=n;j++)
      {
          scanf("%lld",&w[i][j]);

      }
  }

  for(int i=n-1;i>=1;i--)
  {
       for(int j=i;j<=n;j++)
       {

         dp[i]=(dp[i]+dp[i+1]+w[i][j]*factor[n-i])%mod;
       }
  }

  cout<<dp[1];



}

AHOI2001是一种用于处理模式匹配和字符串搜索的经典算法,全称为"Another Happy Odyssey in 2001"。它通常应用于构建高效、空间优化的KMP(Knuth-Morris-Pratt)算法的一种改进版本。这种有限自动机常用于处理字符串搜索问题,尤其是在处理大量文本数据时。 关于题目代码的具体内容,这通常涉及到编程竞赛或算法实现题。通常,你需要编写一段程序,包括定义一个有限状态机(Finite Automaton),处理输入字符串和模式串,并根据AHOI2001算法来查找模式是否在原字符串中。关键部分会涉及如何创建前缀函数表、动态规划和自适应策略。 由于这不是一个直接的答案,下面是一个简化版的代码框架示例(假设用Python): ```python class AhoCorasickAutomaton: def __init__(self, patterns): self.prefix_func = self.build_prefix_function(patterns) def build_prefix_function(self, patterns): # 建立前缀函数表的计算过程... pass def search(self, text): index = 0 for pattern in patterns: while index < len(text) and index + len(pattern) <= len(text): if self.match(text[index:], pattern): return True index += self.prefix_func[pattern] return False def match(self, text, pattern): # 匹配函数,比较两个字符串是否相等... pass # 使用示例: patterns = ['AB', 'AC'] # 输入模式列表 automaton = AhoCorasickAutomaton(patterns) text = 'ABCABCD' # 待搜索的字符串 if automaton.search(text): print("Pattern found") else: print("Pattern not found")
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qinsanma and Code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值