首页 > 其他分享 >hdu 4758 Walk Through Squares(AC自动机+DP,4级)

hdu 4758 Walk Through Squares(AC自动机+DP,4级)

时间:2023-05-21 21:02:26浏览次数:59  
标签:4758 node hdu ch -- AC two each line


Walk Through Squares


Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 234    Accepted Submission(s): 58


Problem Description



hdu 4758 Walk Through Squares(AC自动机+DP,4级)_#include


  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 secified tpravel 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


 



Source


2013 ACM/ICPC Asia Regional Nanjing Online


 



Recommend


liuyiding


 


思路:AC自动机用上失败指针,预处理出转移态。然后DP[x][y][statu][con] x,y点statu 态,包含子串状态为con 的最优值。

        哎,感觉就是AC自动+DP的模板题,居然当时没YY出来,真是失败啊。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
using namespace std;
const int mod=1000000007;
const int mm=109;
const int msize=209;
class AC_Machine
{
  public:
    int ch[msize][2],val[msize],f[msize],sz;
    void clear()
    {
      sz=1;clr(ch[0],0),val[0]=0;
    }
    int idx(char z)
    {
      if(z=='D')return 0;
      if(z=='R')return 1;
    }
    void insert(char*s,int cc)
    {
      int u=0,v,c;
      for(int i=0;s[i];++i)
      {
        c=idx(s[i]);
        if(!ch[u][c])
        {
          clr(ch[sz],0);val[sz]=0;
          ch[u][c]=sz++;
        }
        u=ch[u][c];
      }
      val[u]|=cc;
    }
    void getFail()
    {
      int u,v,r;
      queue<int>Q;
      FOR(i,0,1)
      {
        u=ch[0][i];
        if(u){Q.push(u);f[u]=0; }
      }
      while(!Q.empty())
      {
        r=Q.front();Q.pop();
        val[ r ]|=val[ f[r] ];
        FOR(c,0,1)
        {
          u=ch[r][c];
          if(!u){ ch[r][c]=ch[ f[r] ][c];continue; }
          v=f[r];Q.push(u);
          f[u]=ch[v][c];
        }
      }
    }
   int dp[mm][mm][msize][4];
   void solve(int n,int m)
   { int tmp,now,then;
     clr(dp,0);
     dp[0][0][ val[0] ][0]=1;
     FOR(x,0,n)FOR(y,0,m)
     FOR(k,0,sz-1)FOR(l,0,3)
     {
       if(x+1<=n)///down
       {
         then=ch[k][0];tmp=l|val[then];
         dp[x+1][y][then][tmp]=(dp[x+1][y][then][tmp]+dp[x][y][k][l])%mod;
       }
       if(y+1<=m)
       {
         then=ch[k][1];tmp=l|val[then];
         dp[x][y+1][then][tmp]=(dp[x][y+1][then][tmp]+dp[x][y][k][l])%mod;
       }
     }
     int ans=0;
     FOR(i,0,sz-1)//FOR(j,0,3)两种动作都要有
     {
       ans=(ans+dp[n][m][i][3])%mod;
     }
     printf("%d\n",ans);
   }
};
AC_Machine ac;
char s[2][msize];
int main()
{
  int cas;
   scanf("%d",&cas);

    while(cas--)
    { int n,m;
      scanf("%d%d",&m,&n);
      ac.clear();
      scanf("%s%s",s[0],s[1]);
      ac.insert(s[0],1);
      ac.insert(s[1],2);
      ac.getFail();
      ac.solve(n,m);
    }

}




标签:4758,node,hdu,ch,--,AC,two,each,line
From: https://blog.51cto.com/u_15953788/6320208

相关文章

  • [React] useEffect
    purefunction:单纯返回jsx元素的组件在使用react函数组件时,理论上函数组件只会进行不改变内部状态值的计算,以及返回html代码。一个pure函数就是如此,例如一个函数组件接受一个id作为传入属性,注意这里传入的id并没有发生变化,我们只是简单进行了输出和数学计算://thisisapurefu......
  • 算法学习记录(模拟枚举贪心题单):[NOIP2007]字符串的展开(未AC,明天找bug)
    题目链接https://ac.nowcoder.com/acm/contest/20960/1001解题思路很简单的模拟题,以后写模拟要先分两大类,元素在某个集合中存不存在的问题,再细分。未AC代码#include<iostream>#include<string>usingnamespacestd;//碰到'-'的展开条件:// 1.减号两侧同为小写字母......
  • Ribbon默认负载均衡规则替换为NacosRule
    近期博主在参与一个SpringCloud搭建,版本为Hoxton.SR12,服务注册发现组件为Nacos的老项目时,发现项目负载均衡组件Ribbon的负载均衡规则在某些场景下不够完美,比如新版本上线,需要重启服务。因此写了这边文章与大家分享。在微服务架构中,负载均衡是实现高可用性、高性能和可......
  • oracle中SCN详细解析
    在Oracle数据库中,SCN表示数据库中状态变化的时间点,是一个连续唯一的数字标识符。SCN的类型比较多,本文将会详细介绍控制文件中的SCN、检查点(SCNCheckpoint)、数据文件的起始SCN和终止SCN、归档日志的SCN以及在线日志的SCN,同时描述这些不同类型的SCN之间的关系。控制文件中的SCN在O......
  • 第二十一篇——MACD与KDJ合二为一指标公式怎么编写?(从零起步编写通达信指标公式系列)
    在编写MACD与KDJ合二为一指标公式之前,先来了解一下技术指标共振。常见的技术指标共振有三种类型:单指标多周期共振、单指标多级别共振、多技术指标共振,今天主要介绍第三种。 多技术指标共振是指多个技术指标显示出相似的趋势或信号,这通常被视为市场趋势或价格变化的信号,并表......
  • mac软件最佳资源下载站「macw」
    macw是一个专业的Mac苹果电脑软件下载网站。海量Mac软件,Mac教程技巧,壁纸,字体,模板,插件视频等资源集一身。有众多业界所推崇的主流软件,还有许多你不曾了解的小众精品软件。完美破解,人工测试,及时更新。更多详情:https://www.macw.com/?id=ODA2NCZfJjI3LjE4Ni4xMjUuMTE2......
  • Waves 14 Complete Mac (Waves混音效果全套插件)
    Waves14Complete是一款全功能的音频处理软件套装,包含超过140个插件,可用于各种音频处理和音乐制作任务。这个套装包含了多种不同类型的插件,包括均衡器、压缩器、混响、延迟、合成器、调制器等等。Waves14Complete还提供了许多专业级功能,如自适应限制、自动启动时间校准、360度......
  • Activity中使用Menu
    手机毕竟和电脑不同,它的屏幕空间是十分有限的,如果你的Activity中有大量的菜单需要显示,可以使用Menu来实现。首先在res资源目录下新建一个menu文件夹,并在该文件夹下新加一个文件main.xml 在main.xml中定义菜单选项资源<?xmlversion="1.0"encoding="utf-8"?><menuxmlns:a......
  • Stack Overflow 2017 开发者调查报告(程序员必看)
    最近,StackOverflow发布了一篇2017开发者调查报告,此次在全球有超过64,000名开发者参与调查,分别对其技能、工具、学习趋势等数据进行了统计,比较遗憾的是中国参与调查的开发者很少,只有大概300人左右,所以有些调查结果可能跟中国环境不太相符,不过毫无疑问,这几乎代表了全球技术的......
  • 中文环境下使用 huggingface 模型替换 OpenAI的Embedding 接口
    OpenAI的文本嵌入衡量文本字符串的相关性。嵌入通常用于:搜索(其中结果按与查询字符串的相关性排名)聚类(其中文本字符串按相似性分组)推荐(推荐具有相关文本字符串的项目)异常检测(识别出相关性不大的异常值)多样性测量(分析相似性分布)分类(其中文本字符串按其最相似的标签分类)嵌入是浮......