B. Word Cut
 

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 exactly k splitoperations 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 test(s)
input
ab
ab
2
output
1
input
ababab
ababab
1
output
2
input
ab
ba
2
output
0
Note

The sought way in the first sample is:

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

题意:

给定一个串A,B,正好执行K次操作,每次操作可以把 A串从中间切开,并调换两部分的位置,问最后得到B串共有多少种不同的切法(只要中间有一次不同,即视为不同)

题解:

设定DP[i][0] i次操作后 是B串 的个数

  DP[i][1] i次操作后非B串的个数

  我们先找出任意由A循环(当成一个环)一个串至多能 转化成B串的数量x

  那么 从dp[i-1][0] 上一次操作的B串数量 可以再次转化成B串的数量就是  (x-1) * dp[i-1][0];那么从上一次操作由非B串转化成B串的数量是 (已经求好)  x*(dp[i-1][1]) 

  那么类似计数法 求dp[i][1] 转换到K就好了

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include<vector>
using namespace std ;
typedef long long ll; const int N = + ;
const int mod = 1e9 + ;
ll dp[N][]; // [i][0]经过i次转换 是B串的个数 // [i][1]经过i次转换 是原串的个数
int main() {
char a[N],b[N];
int k;
scanf("%s%s%d",a,b,&k);
int len = strlen(a);
ll x = ,sum;
for(int i = ; i < len; i++) {
sum = ;int cnt= ;
for(int j = i ;j < len; j++) {
if(a[j] != b[cnt++]) sum++;
}
for(int j = ;j < i; j++) if(a[j] != b[cnt++]) sum++;
if(!sum) x++;
}
if(strcmp(a,b) == ) dp[][] = ;
else dp[][] = ;
for(int i=;i<k;i++)
{
dp[i+][]=(x*dp[i][]+(x-)*dp[i][])%mod;
dp[i+][]=((len-x)*dp[i][]+(len-x-)*dp[i][])%mod;
printf("%I64d %I64d\n",dp[i][],dp[i][]);
}
printf("%I64d\n",dp[k][]);
return ;
}

CodeForces 176B - Word Cut 计数DP的更多相关文章

  1. CodeForces 176B Word Cut (计数DP)

    Word Cut Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit St ...

  2. CodeForces 176B Word Cut dp

    Word Cut 题目连接: http://codeforces.com/problemset/problem/176/C Description Let's consider one interes ...

  3. ACM学习历程—CodeForces 176B Word Cut(字符串匹配 && dp && 递推)

    Description Let's consider one interesting word game. In this game you should transform one word int ...

  4. [Codeforces 176B]Word Cut

    Description 题库链接 给你两个字符串 \(S\) 和 \(T\) ,准许你 \(k\) 次操作,每次将字符串左右分成两个非空的部分,再交换位置,问你有多少种不同的操作方法将 \(S\) 串 ...

  5. Codeforces 176B【计数DP】

    题意: 给你两个串s1,s2和一个K, 有一种操作是在一个串切开然后交换位置, 问s1有多少种方法经过K次这样的操作变成s2: 思路: (从来没接触过计数DP...还是太菜...参考了[大牛blog] ...

  6. Codeforces 176B (线性DP+字符串)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...

  7. Codeforces 176B 经典DP

    非常好的一个题目,CF上的DP都比较经典 题意就是 给定一个串A,B,正好执行K次操作,每次操作可以把 A串从中间切开,并调换两部分的位置,问最后得到B串共有多少种不同的切法(只要中间有一次不同,即视 ...

  8. HDU5800 To My Girlfriend 背包计数dp

    分析:首先定义状态dp[i][j][s1][s2]代表前i个物品中,选若干个物品,总价值为j 其中s1个物品时必选,s2物品必不选的方案数 那么转移的时候可以考虑,第i个物品是可选可可不选的 dp[i ...

  9. [DP之计数DP]

    其实说实在 我在写这篇博客的时候 才刚刚草了一道这样类型的题 之前几乎没有接触过 接触过也是平时比赛的 没有系统的做过 可以说0基础 我所理解的计数dp就是想办法去达到它要的目的 而且一定要非常劲非常 ...

随机推荐

  1. 利用POI操作不同版本号word文档中的图片以及创建word文档

    我们都知道要想利用java对office操作最经常使用的技术就应该是POI了,在这里本人就不多说到底POI是什么和怎么用了. 先说本人遇到的问题,不同于利用POI去向word文档以及excel文档去写 ...

  2. C#趣味程序---三色球问题

    问题:若一个口袋中放有12个球,3红3白和6黑,问从袋中随意取8个球,有多少种不同的颜色搭配? using System; namespace ConsoleApplication1 { class ...

  3. Android系统编译【转】

    本文转载自;http://blog.csdn.net/zirconsdu/article/details/8005415 Android编译系统分析 概要 由于android编译系统的复杂和使用了不熟 ...

  4. Linux下安装intellij idea

    1.下载 http://www.jetbrains.com/idea/download/#section=linux 我下载的是不带jdk的版本 2.放入opt目录中 3.解压到usr下面的intel ...

  5. 关于content-type

    content-type 包含了表单类型和边界字符串信息. 关于content-type get请求的headers中没有content-type这个字段 post 的 content-type 有两 ...

  6. QT-helloworld-Qt设计师编写

    前言:Qt设计师界面类就是C++类和ui文件的结合,它将这两个文件一起生成了,而不用再逐一添加. 目标:在对话框中显示出“helloworld”字样. 一.新建项目 1.1 选择项目模板 文件→新建文 ...

  7. ROS-turtlesim

    前言:turtlesim是ros自带的一个功能包,应该是用于基础教学的功能包,帮助新手入门的一个实例,包括:节点,主题,服务以及参数的应用.通过学习使用turtlesim功能包可以了解ros的一些基础 ...

  8. 微信小程序和App的UI设计有什么异同吗?

    大家总是把小程序和App放在一起比,因此我也花时间看了一下小程序的开发指南,尤其是UI部分的设计和原则,今天就拿它和苹果的HIG(Human Interface Guidelines)做个比较,其实两 ...

  9. zabbix_agent代理端监控FTP服务

    (FTP服务器) 监控agent的Linux主机我们在之前就已经做好了,现在直接安装ftp服务即可 (可以看上一遍文章如何搭建监控端agent)  这里不过多演示 [root@agent ~]# yu ...

  10. C# 基础复习 三 XML操作

    XML 可扩展标记语言(所有标签都是自己定义,没有固定格式) 如果要给XML规定格式,可以使用dtd (dtd是什么?你不会自己百度吗) XML主要用来存储数据 XML的要求:     根节点只能有一 ...