ACM学习历程—CodeForces 176B Word Cut(字符串匹配 && dp && 递推)
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 word w = 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 ksplit 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
ab
ab
2
1
ababab
ababab
1
2
ab
ba
2
0
Sample Output
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
题目大意就是问有几种操作方法,能在k次操作下得到目标串。
首先要肯定的是题目给的操作相当于一个循环串的位移。(位移不为0)
这样的话,就可以对目标串的第一位分析了。
记s(now, k)表示经过k次操作后,目标串的第一位在当前串的第now位。(初始k == 0时,由于串是循环串,会出现不止一个now的值不为0)
于是s(now, k) = sum{s(i, k-1)} (i != now)
只要不是本身就在now,都能位移到now。
大一时做这个题,到就只能想到这里,然后进行了O(k*strlen(str)*strlen(str))的暴力运算。
但是上述式子可以进行一定的变形:
s(now, k) = sum - s(now, k-1)
这样的话,只需要记录下sum,就能在O(k*strlen(str))的时间复杂度下完成了。
需要注意的是,由于采用一维dp会导致计算s(now)的时候破坏了前一个状态。
所以此处需要同时保存前一个状态和当前状态。所以开了第二维,且第二维仅保留两次的状态。(此处采用了亦或运算进行优化)
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#define N 1000000007 using namespace std; char from[], to[];
int n, len, s[][], sum; bool Pair(int now)
{
int i = ;
for (;;)
{
if (i == len)
return true;
if (from[now] != to[i])
return false;
i++;
now = (now+) % len;
}
} bool Input()
{
if (scanf("%s", from) == EOF)
return false;
scanf("%s%d", to, &n);
memset(s, , sizeof(s));
len = strlen(from);
sum = ;
for (int i = ; i < len; ++i)
{
if (Pair(i))
{
s[i][] = ;
sum++;
}
}
return true;
} void Work()
{
int tem, state = ;
for (int i = ; i <= n; i++)
{
tem = ;
state = state^;
for (int j = ; j < len; ++j)
{
s[j][state] = sum - s[j][state^];
s[j][state] = (s[j][state]%N+N)%N;
tem += s[j][state];
tem %= N;
}
sum = tem;
}
printf("%d\n", s[][state]);
} int main()
{
//freopen("test.in", "r", stdin);
while (Input())
{
Work();
}
return ;
}
ACM学习历程—CodeForces 176B Word Cut(字符串匹配 && dp && 递推)的更多相关文章
- CodeForces 176B Word Cut (计数DP)
Word Cut Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit St ...
- ACM学习历程—UESTC 1217 The Battle of Chibi(递推 && 树状数组)(2015CCPC C)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1217 题目大意就是求一个序列里面长度为m的递增子序列的个数. 首先可以列出一个递推式p(len, i) = ...
- ACM学习历程—HDU1028 Ignatius and the Princess III(递推 || 母函数)
Description "Well, it seems the first problem is too easy. I will let you know how foolish you ...
- CodeForces 176B Word Cut dp
Word Cut 题目连接: http://codeforces.com/problemset/problem/176/C Description Let's consider one interes ...
- CodeForces 176B - Word Cut 计数DP
B. Word Cut Let's consider one interesting word game. In this game you should transform one word i ...
- ACM学习历程—Codeforces Round #354 (Div. 2)
http://codeforces.com/contest/676 在allzysyz学弟和hqwhqwhq的邀请下,打了我的第三场CF... 毕竟在半夜..所以本来想水到12点就去睡觉的...结果一 ...
- ACM学习历程—CodeForces 590A Median Smoothing(分类讨论 && 数学)
题目链接:http://codeforces.com/problemset/problem/590/A 题目大意是给一个串,头和尾每次变换保持不变. 中间的a[i]变成a[i-1],a[i],a[i+ ...
- ACM学习历程—CodeForces 601A The Two Routes(最短路)
题目链接:http://codeforces.com/problemset/problem/601/A 题目大意是有铁路和陆路两种路,而且两种方式走的交通工具不能在中途相遇. 此外,有铁路的地方肯定没 ...
- [Codeforces 176B]Word Cut
Description 题库链接 给你两个字符串 \(S\) 和 \(T\) ,准许你 \(k\) 次操作,每次将字符串左右分成两个非空的部分,再交换位置,问你有多少种不同的操作方法将 \(S\) 串 ...
随机推荐
- Spring学习八----------Bean的配置之Resources
© 版权声明:本文为博主原创文章,转载请注明出处 Resources 针对于资源文件的统一接口 -UrlResource:URL对应的资源,根据一个URL地址即可创建 -ClassPathResour ...
- client交互技术简单介绍
随着网络应用的不断丰富,client交互技术也如雨后春笋一般,遍地开花. 正是这些技术的支持,我们的互联网世界变得更加丰富多彩.一个浏览器上.不用说是简单的动画效果,就是一个Office应用也能顺畅的 ...
- [转]FPGA网站推荐
1. OPENCORES.ORG这里提供非常多,非常好的PLD了内核,8051内核就可以在里面找到.进入后,选择project或者由http//www.opencores.org/browse.cgi ...
- [转]XMPP基本概念--节(stanza)
本文介绍在XMPP通信中最核心的三个XML节(stanza).这些节(stanza)有自己的作用和目标,通过组织不同的节(stanza),就能达到我们各种各样的通信目的. 首先我们来看一段XMPP流. ...
- 11 linux nginx上安装ecshop 案例
一: nginx上安装ecshop 案例 (1)解压到 nginx/html下 浏览器访问:127.0.0.1/ecshop/index.php 出现错误:not funod file 原因:ngin ...
- C语言的运算符的优先级与结合性+ASCII表
[0]README 0.1) 内容来源于 C程序设计语言, 旨在整理出C语言的运算符的优先级与结合性, 如下图所示(哥子 记了大半年都没有记住,也是醉了,每次都要去翻): Alert)以下内容转自:h ...
- oracle序列sequence
序列 定义一个序列,自动产生连续的整数.也称序列生成器(sequence generator)产生序列号.在多用户环境下该序列生成器特别有用,可生成各返回序列号而不需要磁盘I/O或事务封锁.序列号为O ...
- NoSQL数据库介绍(4)
4 键/值存储 讨论了经常使用的概念.技术和模式后.第一类NoSQL数据存储会在本章进行研究. 键/值存储通常有一个简单的数据模型:一个map/dictionary,同意客户按键来存放和请求 ...
- C#定时检測子线程是否已经完毕
C#定时检測子线程是否已经完毕 class Program { static void Main(string[] args) { //主线程中启动一个支线程,运行doSomething这种一个方法. ...
- EasyPlayerPro Windows播放器全屏模式下GDI显示出现黑屏问题解决
问题来源 2017.12.21 前天有杭州某教育领域客户反馈有部分视频源在全屏模式下显示黑屏: 问题复现 EasyPlayerPro由于没有实现单个窗口完全全屏,故没有暴露该问题,晚上加班,加上单个窗 ...