Codeforces 1120C Compress String(DP)】的更多相关文章

题意:给你一个字符串,有2种消除方式:1:消除一个单独的字母,代价为a.2:s[j]到s[k]是s[1]到s[j - 1]的子串,那么s[j]到s[k]可以消除,代价为b,问最小的代价. 思路:官方题解说的很明白了. 代码: #include <bits/stdc++.h> #define LL long long #define INF 0x3f3f3f3f using namespace std; const int maxn = 5010; int dp[maxn]; int v[max…
题目链接: https://codeforces.com/contest/1120/problem/C 题意: 从前往后压缩一段字符串 有两种操作: 1.对于单个字符,压缩它花费$a$ 2.对于末尾一段字符串,如果这段字符串是已经压缩过字符串的子串,那么可以选择压缩它,花费为$b$ 数据范围: $1\leq |S| \leq 5000$ $1\leq a \leq 5000$ $1\leq b \leq 5000$ 分析: 这道题目我们需要解决一个问题,计算某个子串出现的最早位置 转移,如果这个…
Compress String 时间限制:2000 ms  |  内存限制:65535 KB 难度:3 描写叙述 One day,a beautiful girl ask LYH to help her complete a complicated task-using a new compression method similar to Run Length Encoding(RLE) compress a string.Though the task is difficult, LYH i…
1.5 Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2blc5a3. If the "compressed" string would not become smaller than the original string, your met…
[Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列上可以向上走,其他列不能向上走.可以重复经过同一个点.求从(1,1)出发,经过所有宝藏的最短路径长度 \(n,m,k,q \leq 2 \times 10^5\) 分析 贪心考虑,我们应该按照行一层一层的走.每一行应该从最左的宝藏走到最右的宝藏,或者从最右的宝藏走到最左的宝藏,然后找最近的一个可以向…
Given a string, replace adjacent, repeated characters with the character followed by the number of repeated occurrences. Assumptions The string is not null The characters used in the original string are guaranteed to be ‘a’ - ‘z’ Examples “abbcccdeee…
Codeforces 1120 C 题意:给一个串\(S\),将这个串分成\(t_1..t_m\),如果\(t_i\)在\(t_1..t_{i-1}\)中作为子串出现过,那么这个的代价是\(b\),否则如果\(|t_i|=1\),那么这个的代价是\(a\). 问最少代价. 思路:第一次现场敲对\(Suffix\ AutoMaton\)祭 首先考虑\(dp_i\)表示处理到第\(i\)个位置,最少的代价. 然后向后枚举一个在\(S_{1..i-1}\)中出现过的子串\(S_{i..j}\),转移\…
题目链接: http://codeforces.com/problemset/problem/710/E E. Generate a String time limit per test 2 secondsmemory limit per test 512 megabytes 问题描述 zscoder wants to generate an input file for some programming competition problem. His input is a string co…
/** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: dp[i]表示前i个字符需要的最小次数. dp[i] = min(dp[j]+w(j+1,i)); (0<=j<i); [j+1,i]如果存在循环节(自身不算),那么取最小的循环节x.w = digit((i-j)/x)+x; 否则w = i-j+1; 求一个区间最小循环节: 证明:http://w…
题目链接: E. Generate a String time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard output zscoder wants to generate an input file for some programming competition problem. His input is a string consistin…
题目链接:http://codeforces.com/problemset/problem/710/E 加或者减一个字符代价为x,字符数量翻倍代价为y,初始空字符,问你到n个字符的最小代价是多少. dp[i]表示i个字符的最小代价. 当i为偶数个的时候,由+1或者*2得到. 当i为奇数个的时候,由+1或者*2-1得到. //#pragma comment(linker, "/STACK:102400000, 102400000") #include <algorithm>…
题意:给定 n,x,y,表示你要建立一个长度为 n的字符串,如果你加一个字符要花费 x时间,如果你复制前面的字符要花费y时间,问你最小时间. 析:这个题,很明显的DP,dp[i]表示长度为 i 的字符串的最少花费,当 i 是偶数时,要么再加一个字符,要么从i/2中复制,如果为奇数,要么再加1个字符, 要么从i/2先加一个,再复制.即: 奇数 : dp[i] = min(dp[i-1]+x, dp[i/2+1]+y+x); 偶数 : dp[i] = min(dp[i-1]+x, dp[i/2]+y…
这题的题意就很晦涩.题意是:问有多少种方法,把字符串s划分成不重叠的子串(可以不使用完s的所有字符,但是这些子串必须不重叠),使得t串是所有这些新串的子串.譬如第一个样例,"ababa"和"aba",共有5种方法:{aba}(前3个),{aba}(后3个),{abab},{baba},{ababa}. 先设s的长度为lena,t的长度为lenb. 做法是:用dp[i]表示到i为止,有几种方案数,所以最终答案是dp[lena].然后考虑转移.首先dp[i]至少等于dp…
题意: 给两个串S,T,问能找出多少的S的(a1,b1)(a2,b2)..(ak,bk),使Sa1---Sb1,...Sak---Sbk都包含子串T,其中k>=1,且(a1,b1)...(ak,bk)互不相交. 比如S = "abacaba",T="aba", 当k=1时,(0,6)满足,还有其他只包含一个aba串的也满足,k-2时,(0,2)(3,6)满足,(0,2)(4,6)也满足,(0,3)(4,6)也满足,所以总共有12种. 解法:dp.先用kmp找出…
有方程dp[i]=min(dp[i-1]+A,dp[j]+B):如果s[j+1,i]在s[i,j]中出现,所以我们就是要知道每个子串在s出现的第一个位置,这个可以hash实现或者sam,或者kmp实现. pos[i][j]表示s[i,j]对应的sam的位置,occ[],表示第一次出现的位置. #include<bits/stdc++.h> #define ll long long #define rep2(i,a,b) for(int i=a;i>=b;i--) #define rep(…
题意:你可以对字符串s进行3种操作: 1,在pos位置插入字符ch. 2,删除pos位置的字符. 3,替换pos位置的字符为ch. 问最少需要多少次操作可以把字符s变成字符s1? 思路: 设dp[i][j]为字符串s的前i个字符替换成s1的前j个字符的最小花费.则有三种转移: 1:dp[i - 1][j - 1] + (s[i] != s1[j]) -> dp[i][j] //将s[i]替换为s1[j] (真实位置是j). 2:dp[i - 1][j] + 1 ->dp[i][j] //删除i…
http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits:  5000 MS   Memory Limits:  200000 KB 64-bit interger IO format:  %lld   Java class name:  Main Description A Hill Number is a number whose digits possibly rise and then possibl…
题目链接:http://codeforces.com/contest/706/problem/C #include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=1e5+3; const ll INF=1e18; ll dp[N][2]; string a[N],b[N]; int c[N]; int main() { int n; scanf("%d",&n); for(…
Word Cut 题目连接: http://codeforces.com/problemset/problem/176/C 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 in…
http://codeforces.com/problemset/problem/148/D D. Bag of mice time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The dragon and the princess are arguing about what to do on the New Year's Eve…
题目链接:https://codeforces.com/problemset/problem/191/A 题意: 给出 $n$ 个小写字母组成的字符串,两个字符串如果前者的最后一个字母与后者的首字母相同,那么两者可以连接, 同时要求最后得到的一个长字符串的首尾字母也要相同,求最长的满足要求的字符串的长度是多少. 题解: 这个DP蛮有意思的. 记 $f[x][y]$ 为从第一个字符串到当前字符串,字母 $x$ 到字母 $y$ 的最长字符串的长度. 这样,对于当前的字符串 $s_i$,状态转移可以枚…
题目来源:http://codeforces.com/problemset/problem/55/D Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue wit…
题目链接:http://codeforces.com/problemset/problem/455/A 题目大意:有n个数,每次可以选择删除一个值为x的数,然后值为x-1,x+1的数也都会被删除,你可以获得分值x,求出能获得的最大分值为多少. 解题思路:从小到大排序,去重一下, 用cnt[i]记录一下数字i出现次数.那么可以得到状态转移方程:dp[i]=max(dp[i],dp[j]+cnt[i]*a[i])(j<i&&a[i]-a[j]>1),再用单调队列优化一下就行了O(∩…
题目链接:http://codeforces.com/problemset/problem/702/A 题意: 给你N个数,a[0], a[1], a[2], ....., a[n-1],让你找出最长的连续上升子序列中元素的个数. 思路: 设dp[i]代表以a[i]结尾的连续上升子序列中元素的个数,那么dp[i] = (a[i] > a[i - 1] ? dp[i - 1] + 1 : 1),含义是如果a[i]比a[i-1]大,那么a[i]可以加入到以a[i-1]为尾的最长连续上升子序列末尾,取…
题目链接 http://codeforces.com/problemset/problem/597/C 题意 给出一个n 一个 k 求 n 个数中 长度为k的上升子序列 有多少个 思路 刚开始就是想用dp 复杂度 大概是 O(n ^ 2 * k) T了 但是 思路还是一样的 只是用树状数组 优化了一下 第三层循环 dp[i][j] 表示 第 i 个数 长度为 j 时 那么 dp[i][j] 的状态转移就是 ∑(arr[i] > arr[k] ? : dp[k][j - 1] ) AC代码 #in…
题目链接:http://codeforces.com/problemset/problem/571/B 题意: 给你一个长度为n的数列a[i]. 现在你可以随意改变数字的位置,问你 ∑| a[i] - a[i+k] | 的最小值(1 <= i <= n-k). 题解: 将a[i]拆成若干个子序列s[j],子序列中相邻两数在a[i]中的距离为k. 此时原式 = ∑(子序列s[j]内部之差的和) 显然,要想使子序列s[j]内部之差的和尽可能小,子序列s[j]内部一定为升序. 显然,要想使 ∑(子序…
题目链接:http://codeforces.com/problemset/problem/180/C 题意: 给你一个字符串s,长度为n. 让你将这个字符串变成“前面一段都是大写字母,后面一段都是小写字母”的形式. (也可以全是大写或全是小写) 问你最少改动几个字符. 题解: 表示状态: dp[i][0/1] = min changes 表示考虑到第i个字符,s[i]改成了大写(1)或小写(0)时的最小代价 找出答案: ans = min(dp[n][0], dp[n][1]) 如何转移: d…
传送门:CodeForces - 940E - Cashback 题意:在一个长度为n的数组中,可以分出长度为 k 连续的多个数组b(每个数组 b 的 k 可不相同),然后,可以对每个数组 b 进行删去 k / c 个数的操作: 输出最小的全部数组b的和: 思路:首先要贪心的想到,这个 k 要么等于 c ,要么等于 1 ,才能使总和最小: 所以列出递推方程:hh [ i -1 ] = min(hh[i - 1] + a[ i ]   , hh[ i - c]+sum[ i ] - sum[ i…
codeforces 1185G1. Playlist for Polycarp (easy version)(动态规划) 传送门:https://codeforces.com/contest/1185/problem/G1 题意: 你从学校回到家要T的时间,你现在有n首歌,每首歌的播放时间为ti,编号为gi,你现在想要确定播放一些歌使得你正好用T分钟听完这些歌,且每次连续播放的两首歌编号不同.问你有多少种播放方法,注意顺序不同视为两种方法. 题解: 代码: #include <set> #i…
Codeforces 677D 传送门:https://codeforces.com/contest/677/problem/D 题意: 给你一个n*m的方格图,每个点有一个权值val,现在要求你从坐标(1,1)开始走,要求你从权值为1的点,走到权值为2的点,依次类推,最终走到权值为p的点的最短路径是多少 题解: 分层图dp \[ dp[i][j]表示到达点(i,j)所需要的最短距离是多少\\ dis维护一个纵列上的距离\\ vis维护一个当前走到的位置\\ 转移:dp[r][c] = min(…