首先毋庸置疑這是一道水題
Mahmoud wrote a message s of length n. He wants to send it as a birthday present to his friend Moaz who likes strings. He wrote it on a magical paper but he was surprised because some characters disappeared while writing the string. That’s because this magical paper doesn’t allow character number i in the English alphabet to be written on it in a string of length more than ai. For example, if a1?=?2 he can’t write character ‘a’ on this paper in a string of length 3 or more. String “aa” is allowed while string “aaa” is not.
Mahmoud decided to split the message into some non-empty substrings so that he can write every substring on an independent magical paper and fulfill the condition. The sum of their lengths should be n and they shouldn’t overlap. For example, if a1?=?2 and he wants to send string “aaa”, he can split it into “a” and “aa” and use 2 magical papers, or into “a”, “a” and “a” and use 3 magical papers. He can’t split it into “aa” and “aa” because the sum of their lengths is greater than n. He can split the message into single string if it fulfills the conditions.
A substring of string s is a string that consists of some consecutive characters from string s, strings “ab”, “abc” and “b” are substrings of string “abc”, while strings “acb” and “ac” are not. Any string is a substring of itself.
While Mahmoud was thinking of how to split the message, Ehab told him that there are many ways to split it. After that Mahmoud asked you three questions:
How many ways are there to split the string into substrings such that every substring fulfills the condition of the magical paper, the sum of their lengths is n and they don't overlap? Compute the answer modulo 109?+?7.What is the maximum length of a substring that can appear in some valid splitting?What is the minimum number of substrings the message can be spit in?Two ways are considered different, if the sets of split positions differ. For example, splitting “aa|a” and “a|aa” are considered different splittings of message “aaa”. Input
The first line contains an integer n (1?≤?n?≤?103) denoting the length of the message.
The second line contains the message s of length n that consists of lowercase English letters.
The third line contains 26 integers a1,?a2,?…,?a26 (1?≤?ax?≤?103) — the maximum lengths of substring each letter can appear in. Output
Print three lines.
In the first line print the number of ways to split the message into substrings and fulfill the conditions mentioned in the problem modulo 109??+??7.
In the second line print the length of the longest substring over all the ways.
In the third line print the minimum number of substrings over all the ways.
3 aab 2 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
3 2 2
10 abcdeabcde 5 5 5 5 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
401 4 3
本著熟悉代碼的初衷,我開始寫這道題,然而過程中還出現不少以前不知道的錯誤(哦,對了,本題做法是DP+貪心,自己看看就行了)
下面貼丑陋的代碼
#include<cstdio>#include<cstring>#include<algorithm>#define maxn 1005#define M (int)(1e9+7)#define INF (int)2e9using namespace std;int dp1[maxn];int pro[maxn];int num[26];char s[maxn];int len;int DP1(int pos){ if(pos>=len)return 1; if(dp1[pos])return dp1[pos]; int& ans=dp1[pos]; ans=0; for(int i=pos+1;i<=pro[pos]+1;i++){ ans=(DP1(i)+ans)%M; } return ans;}void init(){ int ch[26],minnum=INF; memset(ch,0,sizeof(ch)); for(int i=0;i<len;i++){ int k=-1; for(int j=i;j<len;j++){ minnum=min(minnum,num[s[j]-'a']); if(j-i+1>minnum){ k=j-1; break; } } pro[i]=(k==-1)?len-1:k; memset(ch,0,sizeof(ch)); minnum=INF; } return;}int main(){ scanf("%d",&len); scanf("%s",s); for(int i=0;i<26;i++) scanf("%d",&num[i]); init(); int max1=0; for(int i=0;i<len;i++){ max1=max(max1,pro[i]-i+1); } int min1=0; for(int i=0;i<len;i++){ i=pro[i]; min1++; } printf("%d/n%d/n%d",DP1(0),max1,min1); return 0;}大概的錯點有如下:
1.memset函數被cstring庫所包含,且參數中的 memset(*type pointer,int ascll,int size)中的第二個是ascll碼,當然直接填入一個字符也行
2.使用e來表示數字時(如2e9,1e+7)時應該將這個數的全部進行強制類型轉換,如:
#define M (int)(1e9+7)否則會發生問題(要是這樣寫的話):
#define M (int)1e9+73.還有就是能不用(或簡化)DP就不用(或簡化),其余還要考慮到問題具有的貪心性質,這個東西可以用擬陣來進行說明,這里不予闡述
這次主要記錄了幾個編程方面的幾個小問題,C++博大精深,要學的還有很多(當然算法也有很多QAQ)
新聞熱點
疑難解答