Gym - 100712D Alternating Strings

http://codeforces.com/gym/100712/attachments

题意:

给出一个01串,现在要切割这个01串,使得每个子串长度都不大于k,并且每个子串不能01交替出现,单个字符是合法的。

思路:
很容易想到用dp去做,但是怎么做呢?

我们可以先预处理一下i~j是否是合法的子串,即是否是01交替出现的串。

接下来我们从尾部开始,d【i】表示i~n所需的最少切割数。那么每次在左边新加入一个数字时,怎么确定它的最少切割数呢?

设f【i】【t】不是01交替的串并且长度是小于等于k的,那么在t点是可以切割的,我们只需要枚举切割点找最小值就可以了。

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<sstream>
 6 #include<vector>
 7 #include<stack>
 8 #include<queue>
 9 #include<cmath>
10 #include<map>
11 using namespace std;
12 typedef long long ll;
13 typedef pair<int,long long> pll;
14 const int INF = 0x3f3f;
15 const int maxn=1000+5;
16
17 int n,k;
18 char str[maxn];
19
20 int f[maxn][maxn];
21 int d[maxn];
22
23 int main()
24 {
25     //freopen("input.txt","r",stdin);
26     int T;
27     scanf("%d",&T);
28     while(T--)
29     {
30         scanf("%d%d",&n,&k);
31         getchar();
32         scanf("%s",str+1);
33
34         memset(f,0,sizeof(f));
35
36         for(int i=1;i<=n;i++)
37         {
38             f[i][i]=1;
39             for(int j=i+1;j<=n;j++)
40             {
41                 if(f[i][j-1] && str[j]!=str[j-1])  f[i][j]=1;
42                 else break;
43             }
44             f[i][i]=0;
45         }
46
47         memset(d,INF,sizeof(d));
48         d[n+1]=-1;
49
50         for(int i=n;i>=1;i--)
51         {
52             for(int j=i;j<=i+k-1&&j<=n;j++)
53             {
54                 if(i==j||!f[i][j])
55                     d[i]=min(d[i],d[j+1]+1);
56             }
57         }
58
59         printf("%d\n",d[1]);
60     }
61     return 0;
62 }
时间: 2024-10-25 18:29:37

Gym - 100712D Alternating Strings的相关文章

Gym 100712L Alternating Strings II(单调队列)

题目链接 Alternating Strings II 题意是指给出一个长度为n的01串,和一个整数k,要求将这个01串划分为很多子串(切很多刀),使得每个子串长度不超过k,且每个字串不是01交替出现的串(例如01, 10, 101, 010, 101010这些都是01交替出现的串),求最少需要切多少次 令F[i]代表前i个数所需要切的最少的刀数(从1开始计数),那么有 F[i]  = min{F[j] | |j + 1, i| <= k 且 [j, i]这个子串不是01交替出现的串} + 1

codeforces gym #101161G - Binary Strings(矩阵快速幂)

题目链接: http://codeforces.com/gym/101161/attachments 题意: 数据范围: 分析: ac代码: #include<bits/stdc++.h> using namespace std; #define ll long long #define pii pair<int,int> const int maxn = 1e5+100; const int mod=1e9+7; struct Node{ ll num[4][4]; Node()

Gym 100247B Similar Strings(哈希+思维)

https://vjudge.net/problem/Gym-100247B 题意: 如果两个字符串通过映射后是一样的,则说明这两个字符串是相似的,现在给出n个字符串,计算出有多少组字符串是相似的. 思路:直接暴力超时了.. 拿样例来说吧,abacaba可以转化成1213121.那么和它相似的字符串也能转化成这个数字串,比如说tetatet,它映射后也能变成1213121. 这样对每个字符串处理一遍就可以了. 1 #include<cstdio> 2 #include<iostream&

GYM 101061 I. Playing with strings(有待更新)

I. Playing with strings time limit per test 2.0 s memory limit per test 64 MB input standard input output standard output Taboush is a 10 year-old school boy. On his birthday, his parents got him a new Alphabet blocks game. Alphabet blocks game consi

ACM: Gym 100935B Weird Cryptography - 简单的字符串处理

Weird Cryptography Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Gym 100935B Description standard input/output Khaled was sitting in the garden under an apple tree, suddenly! , well... you should guess what happened, an

Codeforces Gym 100570 E. Palindrome Query Manacher

E. Palindrome QueryTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100570/problem/E Description De Prezer loves palindrome strings. A string s1s2...sn is palindrome if and only if it is equal to its reverse. De Prezer also love

CodeForces Gym 100500A A. Poetry Challenge DFS

Problem A. Poetry Challenge Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/attachments Description Let’s check another challenge of the IBM ICPC Chill Zone, a poetry challenge. One says a poetry string that starts with a

CodeForces Gym 100935D Enormous Carpet 快速幂取模

Enormous Carpet Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Gym 100935D Description standard input/outputStatements Ameer is an upcoming and pretty talented problem solver who loves to solve problems using computers.

43. Multiply Strings

1. 问题描述 Given two numbers represented as strings, return multiplication of the numbers as a string.Note:The numbers can be arbitrarily large and are non-negative.Converting the input string to integer is NOT allowed.You should NOT use internal librar