http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2607
Mountain Subsequences
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
Coco is a beautiful ACMer girl living in a very beautiful mountain. There are many trees and flowers on the mountain, and there are many animals and birds also. Coco like the mountain so much that she now name some letter sequences as Mountain Subsequences.
A Mountain Subsequence is defined as following:
1. If the length of the subsequence is n, there should be a max value letter, and the subsequence should like this, a1 < ...< ai < ai+1 < Amax > aj > aj+1 > ... > an
2. It should have at least 3 elements, and in the left of the max value letter there should have at least one element, the same as in the right.
3. The value of the letter is the ASCII value.
Given a letter sequence, Coco wants to know how many Mountain Subsequences exist.
输入
For each case there is a number n (1<= n <= 100000) which means the length of the letter sequence in the first line, and the next line contains the letter sequence.
Please note that the letter sequence only contain lowercase letters.
输出
示例输入
4abca
示例输出
4
提示
aba, aca, bca, abca
来源
示例程序
分析:
给你一个长度为n的字符串仅由小写英文字母组成,求满足
a1 < ...< ai < ai+1 < Amax > aj > aj+1 > ... > an
的子串的个数,其实也就是统计所有满足以某一元素为中心左边递增,右边递减的子串的数目,要求该子串
最小长度为3,中心元素左右都至少有一个元素。
题目告诉我们输入串仅包含26个英文字母,这样我们只要枚举每一个位置,然后记录每个位置左边满足
条件的个数,右边满足条件的个数,最后乘起来就是了。关键是我们如何统计左右两边满足的个数呢?
本能的反应告诉我可以用DP来做,主要还是因为有之前做过的求最长递增子序列的基础。可是,
老师告诉我说,这不叫DP,看代码会发现,其实就是个递推求解关系式。我总是觉得,之所以能想到
这里,完全是应用了DP的阶段划分的思想,暂且不论也罢。那么,怎么划分阶段呢?我们定义
数组dp[26],dl[maxn],dr[maxn],dp[c]表示以字符c结尾的满足情况的个数,dl[i]表示第i个位置左边满足
条件的个数,dr[i]表示第i个位置右边满足条件的个数。当然,我们可以发现,dp[s[i]] = (dl[i] + 1),s[i]= c;
1表示s[i]单独一个时满足的情况,dl[i]表示他左边的满足的各种情况加上s[i] 后满足的情况。
注意:
有细心的同学会问,在一个串中如果有相同的字母,那么他们的dp值是否相同呢?答案就在题意中给出了。
题目在要求该子串的时候用了
a1 < ...< ai < ai+1 < Amax > aj > aj+1 > ... > an
这样一个式子,每个元素都表示不同位置的元素,也就是说,在串aaba中应该有aba,aba两个答案,
而不是只有aba一个。因为前面两个子串形式上看虽然相同,但纠其本质,a和a的来源不同,
可以表示为a1ba和a2ba两个答案。
AC代码:
1 #include2 #include 3 #define maxn 100005 4 #define mod 2012 5 using namespace std; 6 7 char str[maxn]; 8 int dp[30],dl[maxn],dr[maxn],s[maxn]; 9 int main()10 {11 int n;12 while(cin>>n)13 {14 cin>>str;15 for(int i=0;i =0;i--)28 {29 for(int j=0;j
官方代码:
1 #include2 #include 3 #include 4 using namespace std; 5 const int maxn = 100000+5; 6 int n; 7 char s[maxn]; 8 int dl[maxn], dr[maxn], dp[26], cnt[26]; 9 const int mod = 2012;10 int main() {11 freopen("data.in", "r", stdin);12 freopen("data.out", "w", stdout);13 while(~scanf("%d", &n)) {14 scanf("%s", s);15 for(int i = 0; i < n; i++) s[i] -= 'a';16 memset(dp, 0, sizeof(dp));17 memset(cnt, 0, sizeof(cnt));18 memset(dl, 0, sizeof(dl));19 memset(dr, 0, sizeof(dr));20 for(int i = 0; i < n; i++) {21 for(int j = 0; j < s[i]; j++) {22 dl[i] += dp[j] + cnt[j];23 dl[i] %= 2012;24 }25 cnt[s[i]] ++;26 cnt[s[i]] %= 2012;27 dp[s[i]] += dl[i];28 dp[s[i]] %= 2012;29 30 }31 32 memset(dp, 0, sizeof(dp));33 memset(cnt, 0, sizeof(cnt));34 35 for(int i = n - 1; i >= 0; i-- ) {36 for(int j = 0; j < s[i]; j ++) {37 dr[i] += dp[j] + cnt[j];38 dr[i] %= 2012;39 }40 cnt[s[i]] ++;41 cnt[s[i]] %= 2012;42 dp[s[i]] += dr[i];43 dp[s[i]] %= 2012;44 }45 int ans = 0;46 for(int i = 0; i < n; i++) {47 ans += dl[i] * dr[i] ;48 ans %= 2012;49 }50 printf("%d\n", ans);51 }52 }
官方数据生成代码:
1 #include2 #include 3 #include 4 using namespace std; 5 const int maxn = 100000+5; 6 int n; 7 char s[maxn]; 8 int dl[maxn], dr[maxn], dp[26], cnt[26]; 9 const int mod = 2012;10 int main() {11 freopen("data.in", "r", stdin);12 freopen("data.out", "w", stdout);13 while(~scanf("%d", &n)) {14 scanf("%s", s);15 for(int i = 0; i < n; i++) s[i] -= 'a';16 memset(dp, 0, sizeof(dp));17 memset(cnt, 0, sizeof(cnt));18 memset(dl, 0, sizeof(dl));19 memset(dr, 0, sizeof(dr));20 for(int i = 0; i < n; i++) {21 for(int j = 0; j < s[i]; j++) {22 dl[i] += dp[j] + cnt[j];23 dl[i] %= 2012;24 }25 cnt[s[i]] ++;26 cnt[s[i]] %= 2012;27 dp[s[i]] += dl[i];28 dp[s[i]] %= 2012;29 30 }31 32 memset(dp, 0, sizeof(dp));33 memset(cnt, 0, sizeof(cnt));34 35 for(int i = n - 1; i >= 0; i-- ) {36 for(int j = 0; j < s[i]; j ++) {37 dr[i] += dp[j] + cnt[j];38 dr[i] %= 2012;39 }40 cnt[s[i]] ++;41 cnt[s[i]] %= 2012;42 dp[s[i]] += dr[i];43 dp[s[i]] %= 2012;44 }45 int ans = 0;46 for(int i = 0; i < n; i++) {47 ans += dl[i] * dr[i] ;48 ans %= 2012;49 }50 printf("%d\n", ans);51 }52 }