算法连载4

Problem Statement

 You just bought a very delicious chocolate bar from a local store. This chocolate bar consists of N squares, numbered 0 through N-1. All the squares are arranged in a single row. There is a letter carved on each square. You are given a string letters. The i-th character of letters denotes the letter carved on the i-th square (both indices are 0-based).

You want to share this delicious chocolate bar with your best friend. At first, you want to give him the whole bar, but then you remembered that your friend only likes a chocolate bar without repeated letters. Therefore, you want to remove zero or more squares from the beginning of the bar, and then zero or more squares from the end of the bar, in such way that the remaining bar will contain no repeated letters.

Return the maximum possible length of the remaining chocolate bar that contains no repeated letters.

Definition

 
Class:ChocolateBar
Method:maxLength
Parameters:string
Returns:int
Method signature:int maxLength(string letters)
(be sure your method is public)
 
 

Constraints

-letters will contain between 1 and 50 characters, inclusive.
-Each character of letters will be a lowercase letter ('a' - 'z').

Examples

0) 
 
"srm"
Returns: 3
You can give the whole chocolate bar as it contains no repeated letters.
1) 
 
"dengklek"
Returns: 6
Remove two squares from the end of the bar.
2) 
 
"haha"
Returns: 2
There are three possible ways:
  • Remove two squares from the beginning of the bar.
  • Remove two squares from the end of the bar.
  • Remove one square from the beginning of the bar and one square from the end of the bar.
3) 
 
"www"
Returns: 1
4) 
 
"thisisansrmbeforetopcoderopenfinals"
Returns: 9

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.     


这个题目蛮简单的,就是要我们找有几个不相同字母。所以每个比过去就行了。

别人的代码:

class ChocolateBar
{
public:
	static int madxLength(strinf str){
		int ans =1;
int pos[30]pre[55];
meset(pos,-1,sizeof(pos));
for(int i=0;i<str.length();i++)
{	
	int c=str[i]-'a';
pre[i]=pos[c];
pos[c]=i;
}
	deque<int> qv ;
qv.push_front(0);
for(int i=1;i<str.length();i++){
	while(!qv.empty())
{	int c=qv.back();
if(pre[v]<=pre[i])
qv.pop_back();
else
break;
}
	qv.push_back(i);
ans=max(ans,i-re[qv.front()]);
}

return ans;
}

};这个代码有点乱还有些东西都编译不出来,好像是编译环境的问题。
还是再来个】
 


这个明显多了


上一篇文章中我们介绍了如何通过遗传算法来解决函数优化问题,本文将介绍如何使用遗传算法来解决组合优化问题。 组合优化问题是指在一组离散的元素中,寻找一个最优的组合方案,使得满足一定的约束条件,并且达到最优化的目标。比如,在旅行商问题中,需要找到一条经过所有城市的最短路径。 下面我们以背包问题为例,来介绍如何使用遗传算法来解决组合优化问题。 背包问题是指有一个容量为W的背包,和n个物品,每个物品有一个重量wi和一个价值vi,需要选择一些物品放入背包中,使得总重量不超过W,同时总价值最大。 我们可以将每个物品视为一个基因,将所有物品的组合视为一个个体,通过遗传算法来不断优化每个个体的适应度,从而找到最优的组合方案。 具体步骤如下: 1. 定义基因型和表现型 我们可以将每个物品看做一个基因,每个个体的基因型就是一个n维的01向量,其中每个元素表示该物品是否被选中,1表示被选中,0表示未被选中。例如,一个基因型为[1, 0, 1, 0, 1]表示选中了第1、3、5个物品。 每个个体的表现型就是选中的物品的集合,可以通过基因型和物品的重量和价值来计算得到。 2. 定义适应度函数 适应度函数可以定义为选中的物品的总价值,但需要满足总重量不超过W的约束条件。如果超过了W,则适应度为0。 3. 初始化种群 我们可以随机生成一些基因型,作为初始种群。 4. 选择操作 选择操作可以使用轮盘赌选择,即按照每个个体的适应度来分配一定的比例,再进行随机选择。 5. 交叉操作 交叉操作可以选择两个基因型,随机选取一个位置,将两个基因型在该位置之后的部分进行交换,得到两个新的基因型。 6. 变异操作 变异操作可以随机选择一个基因型的一个位置,将该位置的值进行取反。 7. 繁殖新种群 通过选择、交叉和变异操作,生成一些新的基因型,作为下一代种群。 8. 判断终止条件 可以设定一个终止条件,例如达到最大迭代次数或者找到最优解。 通过上述步骤,我们可以使用遗传算法来解决背包问题。下一篇文章将介绍如何使用遗传算法来解决排课问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值