CodeForces 176B Word Cut(DP)

题意:给你a串和b串,你能切k次,每次切完将尾部分放在头的前面,问有多少种方案切k次从a串变为b串

思路:令dp[i][0]为砍了i次变成b串的方案数,dp[i][1]为砍了i次变成非b串的方案数,然后预处理一下前缀就可以DP了


#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9+7;
#define LL long long
LL dp[300000][2];
int main()
{string a,b;int k;cin >> a >> b >>k;int lena = a.size();int lenb = b.size();if (a==b)dp[0][0]=1;else dp[0][1]=1;int x = 0;for (int i = 0;i<lena;i++){int flag = 1;for (int j = 0;j<lena;j++){if (a[(i+j)%lena] != b[j]){flag = 0;break;}}if (flag)x++;}for (int i = 0;i<k;i++){dp[i+1][0]=(x*dp[i][1]+(x-1)*dp[i][0])%mod;dp[i+1][1]=((lena-x)*dp[i][0]+(lena-x-1)*dp[i][1])%mod;}printf("%d\n",dp[k][0]);
}

Description

Let's consider one interesting word game. In this game you should transform one word into another through special operations.

Let's say we have word w, let's split this word into two non-empty parts x and y so, that w = xy. A split operation is transforming wordw = xy into word u = yx. For example, a split operation can transform word "wordcut" into word "cutword".

You are given two words start and end. Count in how many ways we can transform word start into word end, if we apply exactlyksplit operations consecutively to word start.

Two ways are considered different if the sequences of applied operations differ. Two operation sequences are different if exists such number i (1 ≤ i ≤ k), that in the i-th operation of the first sequence the word splits into parts x and y, in the i-th operation of the second sequence the word splits into parts a and b, and additionally x ≠ a holds.

Input

The first line contains a non-empty word start, the second line contains a non-empty word end. The words consist of lowercase Latin letters. The number of letters in word start equals the number of letters in word end and is at least 2 and doesn't exceed 1000 letters.

The third line contains integer k (0 ≤ k ≤ 105) — the required number of operations.

Output

Print a single number — the answer to the problem. As this number can be rather large, print it modulo 1000000007(109 + 7).

Sample Input

Input
ab
ab
2
Output
1
Input
ababab
ababab
1
Output
2
Input
ab
ba
2
Output
0

Hint

The sought way in the first sample is:

ab → a|b → ba → b|a → ab

In the second sample the two sought ways are:

  • ababab → abab|ab → ababab
  • ababab → ab|abab → ababab

转载于:https://www.cnblogs.com/q934098774/p/5388694.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/396326.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

如何将React App转换为React Native

I have been working on a lot of mobile projects lately — including Cordova, PhoneGap, React Native, some Ionic and Swift — but I have to say, React Native is by far the best experience in mobile development I have had so far. It has great, web-like d…

HTTP状态码:400\500 错误代码

转自&#xff1a;http://blog.sina.com.cn/s/blog_59b052fa0100it74.html一些常见的状态码为&#xff1a;200 - 服务器成功返回网页404 - 请求的网页不存在503 - 服务不可用详细分解&#xff1a;1xx&#xff08;临时响应&#xff09;表示临时响应并需要请求者继续执行操作的状态…

dhcp服务

安装与配置 配置文件 修改配置文件 复制这个文件到另一端 打开另一端的配置文件 原端输入这些命令可以去掉英文 然后vim进入另一端配置文件 全局配置不在{}内的 分发范围是指哪个ip到哪个ip的范围 指定固定电脑获取固定位置 原端修改配置文件 下面进行启动dhcp 克隆一台虚拟机&…

python数据结构与算法40题_Python数据结构与算法40:递归编程练习题3:ASCII谢尔宾斯基地毯...

注&#xff1a;本文如涉及到代码&#xff0c;均经过Python 3.7实际运行检验&#xff0c;保证其严谨性。本文阅读时间约为7分钟。递归编程练习题3&#xff1a;ASCII谢尔宾斯基地毯谢尔宾斯基地毯谢尔宾斯基地毯是形如上图的正方形分形图案&#xff0c;每个地毯可分为等大小的9份…

使用Python发送电子邮件

by Arjun Krishna Babu通过Arjun Krishna Babu 如何使用Python发送电子邮件 (How to send emails using Python) As a learning exercise, I recently dug into Python 3 to see how I could fire off a bunch of emails. There may be more straightforward methods of doing…

此blog不更了

1转载于:https://www.cnblogs.com/ybai62868/p/5384097.html

Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart)

在接触WebService时值得收藏的一篇文章&#xff1a; 在调试Axis1.4访问WebService服务时&#xff0c;出现以下错误&#xff1a; Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart) 有错误找到错误原因以及发现值得收藏的…

java遍历树结构数据_Java数据结构——二叉树的遍历(汇总)

二叉树的遍历分为深度优先遍历(DFS)和广度优先遍历(BFS)DFS遍历主要有&#xff1a;前序遍历中序遍历后序遍历一、递归实现DFSNode.java:public class Node {private Object data;Node richild;Node lechild;public Object getData() {return data;}public void setData(Object …

vue 移动端头像裁剪_使用vue-cropper裁剪正方形上传头像-阿里云开发者社区

引用方式在组件内使用import { VueCropper } from vue-croppercomponents: {VueCropper,},main.js里面使用import VueCropper from vue-cropperVue.use(VueCropper)基本使用方法ref"cropper":img"option.img":autoCrop"true":fixedNumber"[…

规则引擎 设计 git_引擎盖下的Git

规则引擎 设计 gitby Wassim Chegham由Wassim Chegham 引擎盖下的Git (Git under the hood) Let’s explore some common Git commands, and dive into its internals to see what Git does when you run them.让我们探索一些常见的Git命令&#xff0c;并深入了解其内部&#…

练习题之死锁

public class PrintMain {public static String obj1"obj1";public static String obj2"obj2";public static void main(String[] args) {new Thread(new Runnable() {public void run() {System.out.println(new Date().toString "LockA开始执行&qu…

启用或禁用对 Exchange Server 中的邮箱的 POP3 或 IMAP4 访问

https://docs.microsoft.com/zh-cn/Exchange/clients/pop3-and-imap4/configure-mailbox-access?viewexchserver-2019 记录下转载于:https://www.cnblogs.com/amoy9812/p/9875426.html

java有什么压力_编程语言的心智负担!你学编程得有多大的压力快来测试一下...

很多编程语言对比的文章&#xff0c;总喜欢比较各种编程语言的性能、语法、IO模型。本文将从心智负担这个角度去比较下不同的编程语言和技术。内存越界如&#xff1a;C语言、C(C with class)C/C可以直接操作内存&#xff0c;但编程必须要面对内存越界问题。发生内存越界后&…

什么叫有效物理网卡_如何区分虚拟网卡和物理网卡?-阿里云开发者社区

一、什么是物理网卡和虚拟网卡&#xff1f;图示如下&#xff1a;红色部分包含VMWare的为虚拟网卡。通常&#xff0c;我们部署VMWare虚拟机、VMSphere虚拟集群、XenCenter虚拟集群是都会涉及虚拟网卡。二、辨别物理网卡和虚拟网卡的应用场景场景一&#xff1a;一般部署虚拟集群的…

算法复杂度的表示法_用简单的英语算法:时间复杂度和Big-O表示法

算法复杂度的表示法by Michael Olorunnisola通过Michael Olorunnisola 用简单的英语算法&#xff1a;时间复杂度和Big-O表示法 (Algorithms in plain English: time complexity and Big-O notation) Every good developer has time on their mind. They want to give their us…

Android Studio 开始运行错误

/********************************************************************************* Android Studio 开始运行错误* 说明&#xff1a;* 打开Android Studio就抛出这个错误。* * 2017-4-1 深圳 南…

IOS 计步器

这篇博客介绍的是当前比较流行的“计步器”-只是简单的知识点 计步器的实现在IOS8开始进行了改变。 但是我会对之前之后的都进行简单介绍。 IOS 8 - // // ViewController.m // CX 计步器 // // Created by ma c on 16/4/12. // Copyright © 2016年 bjsxt. All rights…

vue学习之二ECMAScript6标准

一、ECMAScript6标准简述 ECMAScript 6.0&#xff08;以下简称 ES6&#xff09;是 JavaScript 语言的下一代标准&#xff0c;已经在 2015 年 6 月正式发布了。它的目标&#xff0c;是使得 JavaScript 语言可以用来编写复杂的大型应用程序&#xff0c;成为企业级开发语言。 1.1E…

抖音吸粉_抖音吸粉5大实用方法首次分享!轻松实现粉丝10000+

抖音&#xff0c;是一款可以拍短视频的音乐创意短视频社交软件&#xff0c;该软件于2016年9月上线&#xff0c;是一个专注年轻人音乐短视频社区。用户可以通过这款软件选择歌曲&#xff0c;拍摄音乐短视频&#xff0c;形成自己的作品。抖音APP仅推出半年&#xff0c;用户量就突…

mapper mysql 主键_实现通用mapper主键策略兼容mysql和oracle

【原创文章&#xff0c;转载请注明原文章地址&#xff0c;谢谢&#xff01;】1.直接用官方提供的注解方法是无法达到兼容效果的2.跟踪源码看看是否有其他方法3.这里有个genSql&#xff0c;可以看一下这个类4.创建一个自定义的处理类实现GenSql(代码中是我实际项目中用到的策略&…