HDU4758AC自动机DP

本文探讨了一个特定的队列方阵问题,即在一个M*N的网格中,从左上角到右下角,遵循特定的动作序列约束,计算所有可能的路径数量。通过使用状态压缩和动态规划的方法,确保了算法的高效性和准确性。

On the beaming day of 60th anniversary of NJUST, as a military college which was Second Artillery Academy of Harbin Military Engineering Institute before, queue phalanx is a special landscape.

Here is a M*N rectangle, and this one can be divided into M*N squares which are of the same size. As shown in the figure below:
01–02–03–04
|| || || ||
05–06–07–08
|| || || ||
09–10–11–12
Consequently, we have (M+1)*(N+1) nodes, which are all connected to their adjacent nodes. And actual queue phalanx will go along the edges.
The ID of the first node,the one in top-left corner,is 1. And the ID increases line by line first ,and then by column in turn ,as shown in the figure above.
For every node,there are two viable paths:
(1)go downward, indicated by ‘D’;
(2)go right, indicated by ‘R’;
The current mission is that, each queue phalanx has to walk from the left-top node No.1 to the right-bottom node whose id is (M+1)*(N+1).
In order to make a more aesthetic marching, each queue phalanx has to conduct two necessary actions. Let’s define the action:
An action is started from a node to go for a specified travel mode.
So, two actions must show up in the way from 1 to (M+1)*(N+1).

For example, as to a 3*2 rectangle, figure below:
01–02–03–04
|| || || ||
05–06–07–08
|| || || ||
09–10–11–12
Assume that the two actions are (1)RRD (2)DDR

As a result , there is only one way : RRDDR. Briefly, you can not find another sequence containing these two strings at the same time.
If given the N, M and two actions, can you calculate the total ways of walking from node No.1 to the right-bottom node ?
Input The first line contains a number T,(T is about 100, including 90 small test cases and 10 large ones) denoting the number of the test cases.
For each test cases,the first line contains two positive integers M and N(For large test cases,1<=M,N<=100, and for small ones 1<=M,N<=40). M denotes the row number and N denotes the column number.
The next two lines each contains a string which contains only ‘R’ and ‘D’. The length of string will not exceed 100. We ensure there are no empty strings and the two strings are different.
Output For each test cases,print the answer MOD 1000000007 in one line.
Sample Input

2
3 2
RRD
DDR
3 2
R
D
Sample Output

1
10

en或一下是关键(广搜保证正确性),AC自动机套DP的基本方法:如果要出现模板串就状态压缩。如果要不出现,DP时就不要走到那个点。然后fail直接接儿子下面是DP的关键。
要输出方案的话,就DFS一遍。

using namespace std;
#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
#define r(x) scanf("%d",&x)
#define F(i,a,b) for(int i=a;i<=b;i++)
#define N 204
#define ll long long
int dp[102][102][242][4];
char s1[N*2],s2[N*2];
int r1[N*2],r2[N*2],r3[N*2];
int fail[N*2];
int en[N*2];
ll ans,mod=1e9+7;
int n,m,l1,l2;
bool flag;
int son[N*2][2];
int sz,root;
bool chg(char x)
{
    if(x=='R') return 1;else return 0;
}
void swp()
{
    for(int i=1;i<=l1;i++) r3[i]=r1[i];
    for(int i=1;i<=l2;i++) r1[i]=r2[i];
    for(int i=1;i<=l1;i++) r2[i]=r3[i];
    swap(l1,l2); 
}
bool vis[N*2];
void init()
{
    memset(son,0,sizeof(son));
    memset(dp,0,sizeof(dp));
    memset(fail,0,sizeof(fail));
    sz=1;root=1;
    memset(vis,0,sizeof(vis));
    l1=0;l2=0;
    flag=0;n=m=0;ans=0;
    memset(en,0,sizeof(en));
}
void build(void)
{
    queue<int>q;
    while(!q.empty())q.pop();
    q.push(1);
    fail[1]=0;
    while(!q.empty())
    {
        int ind=q.front();
        q.pop();
        for(int i=0;i<=1;i++)
        if(son[ind][i])
        {
            int it=fail[ind];
            while(!son[it][i]&&it) it=fail[it];
            if(!it)it=1;else it=son[it][i];
            fail[son[ind][i]]=it;
            en[son[ind][i]]|=en[it];//之前的特判,特盘错了,那是不对的。 
            if(!vis[son[ind][i]])q.push(son[ind][i]);
        }
    }
    for(int i=1;i<=sz;i++)
    {
        for(int j=0;j<=1;j++)
        {
            if(!son[i][j])
            {
                int it=fail[i];
                while(it&&!son[it][j])it=fail[it];
                if(!it)it++;else it=son[it][j];
                son[i][j]=it;
            }
        }
    }
}
void onedance()
{
    init();
    r(n);r(m);scanf("%s",s1);scanf("%s",s2);
    l1=strlen(s1);l2=strlen(s2);
    F(i,0,l1-1){r1[i+1]=chg(s1[i]);}
    F(j,0,l2-1){r2[j+1]=chg(s2[j]);}
    {
        int p=1;
        for(int i=1;i<=l1;i++)
        {
            if(!son[p][r1[i]])son[p][r1[i]]=++sz;
            p=son[p][r1[i]];
        }
        en[p]=1;
    }
    int p=1;
    for(int i=1;i<=l2;i++)
    {
        if(!son[p][r2[i]])son[p][r2[i]]=++sz;
        p=son[p][r2[i]];
    }
    en[p]=2;
    build();
    dp[0][0][1][0]=1;
    for(int len=0;len<n+m;len++)
    {
        for(int i=max(len-n,0);i<=min(len,m);i++)//DP范围 
        {
            for(int k=0;k<=3;k++)
            {
                for(int j=1;j<=sz;j++)
                {
                    for(int sy=0;sy<=1;sy++)
                    {
                        int p=son[j][sy];
                        dp[len-i+sy][i+1-sy][p][k|en[p]]=(dp[len-i+sy][i+1-sy][p][k|en[p]]+dp[len-i][i][j][k])%mod;
                    }
                }
            }
        }
    }
    ans=0;
    for(int i=1;i<=sz;i++)
    {
        ans=(ans+dp[n][m][i][3])%mod;
    }
    cout<<ans<<endl;
}
int main()
{
    int Despacito;r(Despacito);while(Despacito--)onedance();
}
【电动汽车充电站有序充电调度的分散式优化】基于蒙特卡诺和拉格朗日的电动汽车优化调度(分时电价调度)(Matlab代码实现)内容概要:本文介绍了基于蒙特卡洛和拉格朗日方法的电动汽车充电站有序充电调度优化方案,重点在于采用分散式优化策略应对分时电价机制下的充电需求管理。通过构建数学模型,结合不确定性因素如用户充电行为和电网负荷波动,利用蒙特卡洛模拟生成大量场景,并运用拉格朗日松弛法对复杂问题进行分解求解,从而实现全局最优或近似最优的充电调度计划。该方法有效降低了电网峰值负荷压力,提升了充电站运营效率与经济效益,同时兼顾用户充电便利性。 适合人群:具备一定电力系统、优化算法和Matlab编程基础的高校研究生、科研人员及从事智能电网、电动汽车相关领域的工程技术人员。 使用场景及目标:①应用于电动汽车充电站的日常运营管理,优化充电负荷分布;②服务于城市智能交通系统规划,提升电网与交通系统的协同水平;③作为学术研究案例,用于验证分散式优化算法在复杂能源系统中的有效性。 阅读建议:建议读者结合Matlab代码实现部分,深入理解蒙特卡洛模拟与拉格朗日松弛法的具体实施步骤,重点关注场景生成、约束处理与迭代收敛过程,以便在实际项目中灵活应用与改进。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值