sdut 2840 Best string Orz~ (dp)

题目

题意:有n1个o, n2个r, n3个z, n4个~, 求有多少种组合使 组合出来的字符串的任意前缀都满足 o的个数>=r的个数,

r的个数>=z的个数 ……………………

思路:递推,枚举用四重循环控制orz~的个数符合题意, 然后当前个数的orz~等于之前orz~分别少一个推过来的,所以相加上,

注意之前可能orz~的某一个没有。

下面的代码是看了标程之后写出来的。


 1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <queue>
5 #include <cmath>
6 #include <algorithm>
7 using namespace std;
8 #define LL long long
9
10 int main()
11 {
12 int n1, n2, n3, n4;
13 int i1, i2, i3, i4;
14 LL d[11][11][11][11];
15 d[0][0][0][0] = 1;
16 for(i1 = 0; i1 < 10; i1++)
17 for(i2 = 0; i2 <= i1; i2++)
18 for(i3 = 0; i3 <= i2; i3++)
19 for(i4 = 0; i4 <= i3; i4++)
20 {
21 if(i1) d[i1][i2][i3][i4] += d[i1-1][i2][i3][i4];
22 if(i2) d[i1][i2][i3][i4] += d[i1][i2-1][i3][i4];
23 if(i3) d[i1][i2][i3][i4] += d[i1][i2][i3-1][i4];
24 if(i4) d[i1][i2][i3][i4] += d[i1][i2][i3][i4-1];
25 }
26 while(cin>>n1>>n2>>n3>>n4)
27 {
28 if(n1==0&&n2==0&&n3==0&&n4==0) break;
29 cout<<d[n1][n2][n3][n4]<<endl;
30 }
31 return 0;
32 }

sdut 2840 Best string Orz~ (dp),布布扣,bubuko.com

时间: 2024-08-25 20:41:59

sdut 2840 Best string Orz~ (dp)的相关文章

[LeetCode] Interleaving String(dp)

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", return false. 分析:非常好的DP的训练题,

[HDOJ - 5282] Senior&#39;s String 【DP】

题目链接:BZOJ - 5282 题目分析 LCS 就是用经典的 O(n^2) DP 解决,f[i][j] 表示 x 串前 i 个字符与 y 串前 j 个字符的 LCS 长度. f[i][j] = max(f[i - 1][j], f[i][j - 1]); if (x[i] == y[j]) f[i][j] = max(f[i][j], f[i - 1][j - 1] + 1); 然后再设置一个状态 g[i][j], 表示 x 串的前 i 个字符中,有多少个长为 f[i][j] 的子序列同时也

Codeforces 56D Changing a String 编辑距离 dp

题目链接:点击打开链接 编辑距离,,== 一边dp一边记录前驱太累,,还是dp后找路径大法好 #include<iostream> #include<cstdio> #include<vector> #include<string.h> using namespace std; #define ll int #define N 1010 char s[N], t[N]; int dp[N][N], n, m; // 0为插入 1为删除 2 3为替换 stru

leetcode_1048. Longest String Chain_[DP,动态规划,记忆化搜索]

https://leetcode.com/problems/longest-string-chain/ Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A w

Interleaving String [leetcode] DP

dp[k1][k2]:s1[0...k1-1]和s2[0...k2-1]能否合成s3[0...k1 + k2 - 1] bool isInterleave(string s1, string s2, string s3) { if (s3.size() != s1.size() + s2.size()) return false; vector<vector<bool>> dp(s1.size() + 1); for (int i = 0; i < dp.size(); i+

codeforces 825F F. String Compression dp+kmp找字符串的最小循环节

/** 题目: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

HDU 4055 Number String 计数DP

设dp[i][j]表示以j开头的,长度为i的排列的数目. 从字符串的后面到前面DP就得出答案了. #include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> using namespace std; const int MAXN=1010; const int MOD=1000000007; char str[MAXN]; int dp[MAXN][MAXN]; in

Hdu 3336 Count the String(DP+KMP)(好题)

题意:对于长度为len的字符串,我们知道它包含有len个前缀,现在要你统计出这个字符串里面,包含这些前缀的总个数. 思路:这题可以运用KMP的next数组来解,不过也太难想了吧orz,为了用next解这题想那么多也不算是很好的方法orz. 如何根据next数组的性质来解这道题,next数组的值是当前子串的后缀与前缀匹配的个数,所以根据这个性质把题待求的对象改一下:求每种字母作为结尾的串在原串中出现的次数.从最后一个字母分析,首先字符串本身就是以last字母作结,next[last]=x,所以又找

hdu 4055 Number String(dp)

Problem Description The signature of a permutation is a string that is computed as follows: for each pair of consecutive elements of the permutation, write down the letter 'I' (increasing) if the second element is greater than the first one, otherwis