先记录一下今天做题的思考,今天主要是做的stl的题,大部分都用到了map,关于map我发现自己之前的理解还不是很深入,没有思考过为什么要出现map这个容器,map就是一个映射关系,key和value的映射关系,这就和数组很类似了,数组是通过下标来查找数组元素,但是数组下标元素只能是整形,而map就相当于一个可以任意定义数组下标元素类型的数组,通过key来查找value,map也是可以写成数组的形式,用key当做数组下标。
例如

map<string,string>str;
str[s2]=s1;
就是下标为s2,把s1赋给下标为s2的数组

还有就是对于迭代器的理解::iterator 对于多数stl容器要对所有元素遍历就要用到迭代器,但是对于vector就不用了,vector可以通过数组下标来进行访问。

然后就总结一下贪心算法还有一些自己的理解与思考。
在求最优解问题的过程中,依据某种贪心标准,从问题的初始状态出发,直接去求每一步的最优解,通过若干次的贪心选择,最终得出整个问题的最优解,这种求解方法就是贪心算法。
从贪心算法的定义可以看出,贪心算法不是从整体上考虑问题,它所做出的选择只是在某种意义上的局部最优解,而由问题自身的特性决定了该题运用贪心算法可以得到最优解。
如果一个问题可以同时用几种方法解决,贪心算法应该是最好的选择之一。也是复杂度最低的一个算法。
贪心算法一般分为以下五个步骤
(1)候选集合A
(2)解集合S
(3)解决函数solution
(4)选择函数select
(5)可行函数feasible

一个字典序问题 (poj3617)
Description

FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Year" competition. In this contest every farmer arranges his cows in a line and herds them past the judges.
The contest organizers adopted a new registration scheme this year: simply register the initial letter of every cow in the order they will appear (i.e., If FJ takes Bessie, Sylvia, and Dora in that order he just registers BSD). After the registration phase ends, every group is judged in increasing lexicographic order according to the string of the initials of the cows’ names.
FJ is very busy this year and has to hurry back to his farm, so he wants to be judged as early as possible. He decides to rearrange his cows, who have already lined up, before registering them.
FJ marks a location for a new line of the competing cows. He then proceeds to marshal the cows from the old line to the new one by repeatedly sending either the first or last cow in the (remainder of the) original line to the end of the new line. When he’s finished, FJ takes his cows for registration in this new order.
Given the initial order of his cows, determine the least lexicographic string of initials he can make this way.

Input

  • Line 1: A single integer: N
  • Lines 2…N+1: Line i+1 contains a single initial (‘A’…‘Z’) of the cow in the ith position in the original line

Output

The least lexicographic string he can make. Every line (except perhaps the last one) contains the initials of 80 cows (‘A’…‘Z’) in the new line.

Sample Input

6
A
C
D
B
C
B
Sample Output

ABCBCD

贪心算法
贪心算法
有一个坑就是字符是一个一个输入的不是整体读入,这是一个坑。
思路是比较简单,就是无论T的末尾有多大,只要做到T的开头字典序较小就行,这样就可以使用贪心算法,
■不断取S的开头和末尾中较小的一个字符放到T的末尾。
■这个算法已经接近正确了,只是针对S的开头和末尾字符相同的情形还没因为我们希望能够尽早使用更小的字符,所以就要比较下一个字符的大小相同,因此就有如下算法:
■按照字典序比较S和将S反转后的字符串S’。
■如果S较小,就从S的开头取出一个文字,追加到T的末尾。
■如果S较小,就从S的末尾取出一个文字,追加到T的末尾。
(如果相同则取哪个都可以)

#include <stdio.h>
#include <string.h>
char s[2002];
int main(){
int n;
	while(scanf("%d",&n)!=EOF)
	{
		for(int i=0;i<n;i++)
		{
			scanf(" %c",&s[i]);	//前面的空格用来匹配空白符号 
		}
		int a=0,b=n-1;
		int ans=0;
		while(a<=b)
		{
			bool left=false;		
			for(int i=0;a+i<=b;i++)
			{		//字符串反转比较 
				if(s[a+i]<s[b-i])
				{
					left=true;
					break;
				}
				else if(s[a+i]>s[b-i])
				{
					left=false;
					break;
				}
			}
			if(left) 
			 putchar(s[a++]);
			else putchar(s[b--]); 
			ans++;
			if(ans==80)
			{
				printf("\n");
				ans=0;
			} 	
		}	
		  printf("\n");	
	}
	return 0;

贪心就是寻求最优子结构的一个思路。
明天计划吧经典例题看一看,不懂的认真思考一下。加油!!!

相关文章:

猜你喜欢
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案