UPC Fstring字符串(基本状态转移)

本文深入探讨了递归算法中的一种特殊情况,即在字符串生成过程中避免特定字母组合连续出现的算法设计。通过分析不同状态下的字母组合可能性,文章提出了一个递推公式,并提供了详细的代码实现,帮助读者理解递归算法的实际应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

既然不能存在ABC三个字母连续出现的情况,那么假定每一个字母都需要由在他之前的两个字母决定,那么就会有两种情况:前两个字母相等和前两个字母不等。

如果前两个字母相等,那么那两个字母之前的组合可能性都是一样的,后一个字母的状态只相当于复制了前一个状态,比如A到AA,B到BB,C到CC,都是三种情况,那么第三个字母是任意情况。

如果前两个字母不等,那么就需要后一个字母的状态减去前一个字母的状态(减去相等情况),第三个字母只能是前两个字母里选一个,是两种情况。

所以每一个状态都由两个部分组成,因此存在递推式DP[i]=2*(DP[i-1]-DP[i-2])+3*DP[i-2]

//#include<pch.h>
#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
#include <map>
#include <algorithm>
#include <stack>
#include <iomanip>
#include <cstring>
#include <cmath>
#define DETERMINATION main
#define lldin(a) scanf_s("%lld", &a)
#define println(a) printf("%lld\n", a)
#define reset(a, b) memset(a, b, sizeof(a))
const int INF = 0x3f3f3f3f;
using namespace std;
const double PI = acos(-1);
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int mod = 1000000007;
const int tool_const = 19991126;
const int tool_const2 = 2000;
inline ll lldcin()
{
    ll tmp = 0, si = 1;
    char c;
    c = getchar();
    while (c > '9' || c < '0')
    {
        if (c == '-')
            si = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
    {
        tmp = tmp * 10 + c - '0';
        c = getchar();
    }
    return si * tmp;
}
///Untersee Boot IXD2(1942)
/**Although there will be many obstructs ahead,
the desire for victory still fills you with determination..**/
/**Last Remote**/
ll dp[50000];
int DETERMINATION()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    ll n;
    cin >> n;
    dp[0] = 0, dp[1] = 3, dp[2] = 9;
    for (int i = 3; i <= n; i++)
        dp[i] = (3 * dp[i - 2])+ (2 * (dp[i - 1] - dp[i - 2]));
    cout << dp[n] << endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值