gym-101343B-So You Think You Can Count?

本文介绍了一种使用动态规划解决字符串分段问题的方法,目标是将输入字符串分割成多个子串,确保每个子串内的字符都不重复,并计算出所有可能的分割方式数量。文章通过实例详细解释了算法的实现过程。

 

 1 /*
 2 把一个字符串分成若干段,每一段里面的字符不能重复,问有多少种分法
 3 动态规划,定义dp 表示字符串前n个字母的分法种数,先预处理字符串,对于每个字符,
 4 计算出以这个字符为结尾的无重复字符的一段最长的长度,第i个字符对应的长度记为f[i]
 5 然后可以得出递推式:
 6 dp[0]=1;
 7 dp[i]=dp(i-j) (1<=j<=f[i])
 8 */
 9 #include <bits/stdc++.h>
10 using namespace std;
11 int dp[10005];
12 int f[10005];
13 bool vis[10];
14 const int mod=1e9+7;
15 int main()
16 {
17     string s;
18     int n;
19     cin>>n>>s;
20     memset(dp,0,sizeof(dp));
21     memset(f,0,sizeof(f));
22     for(int i=0;i<n;i++)
23     {
24         memset(vis,0,sizeof(vis));
25         int cnt=0;
26         for(int j=i;j>=0;j--)
27         {
28             if(vis[s[j]-'0'])
29                 break;
30             cnt++;
31             vis[s[j]-'0']=1;
32         }
33         f[i+1]=cnt;
34     }
35     dp[0]=1;
36     for(int i=1;i<=n;i++)
37     {
38         int sum=0;
39         for(int j=1;j<=f[i];j++)
40         {
41             sum=(sum+dp[i-j])%mod;
42         }
43         dp[i]=sum;
44     }
45     cout<<dp[n]%mod<<endl;
46 }

 

转载于:https://www.cnblogs.com/kearon/p/7214814.html

统计下面的英文文章,各英语单词出现的个数,找出前五高频词(出现频率最高的前五个)。对前三的高频词分别替换为approach similar evaluation。可能用到的方法 split count。 That must be the story of innumerable couples, and the pattern of life of life it offers has a homely grace. It reminds you of a placid rivulet, meandering smoothly through green pastures and shaded by pleasant trees, till at last it falls into the vasty sea; but the sea is so calm, so silent, so indifferent, that you are troubled suddenly by a vague uneasiness. Perhaps it is only by a kink in my nature, strong in me even in those days, that I felt in such an existence,the share of the great majority, something a miss. I recognized its social value. I saw its ordered happiness, but a fever in my blood asked for a wilder course. There seemed to me something alarming in such easy delights. In my heart was desire to live more dangerously. I was not unprepared for jagged rocks and treacherous, shoals it I could only have change-change and the excitement of unforeseen. Start your day with a positive mind. Not all tasks that you have listed are easy. This may include talking to your boss about a raise,tackling a complicated projector even just going to the gym. You may start thinking that your boss will say 'No' or about all the obstacles you are going to face on the project. You might think going to the gym is too tiring and better not do it. Abraham Lincoln said, "We can complain because rose bushes have thorns, or rejoice because thorn bushes have roses. 用python谢,更适用于新手,要流程图
最新发布
05-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值