HDOJ1048:The Hardest Problem Ever——字符串处理

Problem Description
Julius Caesar lived in a time of danger and intrigue. The hardest situation Caesar ever faced was keeping himself alive. In order for him to survive, he decided to create one of the first ciphers. This cipher was so incredibly sound, that no one could figure it out without knowing how it worked.
You are a sub captain of Caesar’s army. It is your job to decipher the messages sent by Caesar and provide to your general. The code is simple. For each letter in a plaintext message, you shift it five places to the right to create the secure message (i.e., if the letter is ‘A’, the cipher text would be ‘F’). Since you are creating plain text out of Caesar’s messages, you will do the opposite:

Cipher text
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Plain text
V W X Y Z A B C D E F G H I J K L M N O P Q R S T U

Only letters are shifted in this cipher. Any non-alphabetical character should remain the same, and all alphabetical characters will be upper case.

Input
Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following description, and there will be no blank lines separating data sets. All characters will be uppercase.

A single data set has 3 components:

Start line – A single line, “START”

Cipher message – A single line containing from one to two hundred characters, inclusive, comprising a single message from Caesar

End line – A single line, “END”

Following the final data set will be a single line, “ENDOFINPUT”.

Output
For each data set, there will be exactly one line of output. This is the original message by Caesar.

Sample Input
START
NS BFW, JAJSYX TK NRUTWYFSHJ FWJ YMJ WJXZQY TK YWNANFQ HFZXJX
END
START
N BTZQI WFYMJW GJ KNWXY NS F QNYYQJ NGJWNFS ANQQFLJ YMFS XJHTSI NS WTRJ
END
START
IFSLJW PSTBX KZQQ BJQQ YMFY HFJXFW NX RTWJ IFSLJWTZX YMFS MJ
END
ENDOFINPUT

Sample Output
IN WAR, EVENTS OF IMPORTANCE ARE THE RESULT OF TRIVIAL CAUSES
I WOULD RATHER BE FIRST IN A LITTLE IBERIAN VILLAGE THAN SECOND IN ROME
DANGER KNOWS FULL WELL THAT CAESAR IS MORE DANGEROUS THAN HE

简单的字符串处理问题,好久没有A题目了,热热身。
注意字符串的判断方式。要在赋值给char后再通过strcmp()函数比较。
读题目,搞清楚到底哪一行是明文,哪一行是密文。
没什么好说的,一次水过。
代码:

#include
#include
#include
int main()
{
	char passwd[201];
	int i, l, cache;
	while (gets(passwd))
	{
		if (strcmp(passwd, "START") == 0)
			continue;
		if (strcmp(passwd, "END") == 0)
			continue;
		if (strcmp(passwd, "ENDOFINPUT") == 0)
			break;
		l = strlen(passwd);
		for (i = 0; i = 70 && passwd[i] = 65)
			{
				passwd[i] += 21;
			}
		}
		for (i = 0; i 
	

POJ3785:The Next Permutation——全排列问题

Description

For this problem, you will write a program that takes a (possibly long) string of decimal digits, and outputs the permutation of those decimal digits that has the next larger value (as a decimal number) than the input number. For example:

123 -> 132
279134399742 -> 279134423799

It is possible that no permutation of the input digits has a larger value. For example, 987.
Input

The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. Each data set is a single line that contains the data set number, followed by a space, followed by up to 80 decimal digits which is the input value.
Output

For each data set there is one line of output. If there is no larger permutation of the input digits, the output should be the data set number followed by a single space, followed by the string BIGGEST. If there is a solution, the output should be the data set number, a single space and the next larger permutation of the input digits.
Sample Input

3
1 123
2 279134399742
3 987
Sample Output

1 132
2 279134423799
3 BIGGEST


在STL下瞬间变水题……

输入数组时用scanf()而不用gets()。原因是scanf()遇到空格时认为输入结束,而gets()会输入空格:样例显然是以空格而非 结束标志来判断的。所以,乖乖scanf()吧。

还有就是注意格式,这题又PE了,郁闷……看了看是IDE坏掉了。

放上代码:

#include
#include
#include
char num[1010];
using namespace std;
int main()
{
	int p, n, cache;
	scanf("%d", &p);
	while (p--)
	{
		memset(num, 0, sizeof(num));
		cache = n = 0;
		scanf("%d", &cache);
		scanf("%s", num);
		printf("%d ", cache);
		if (next_permutation(num, num + strlen(num)))
			puts(num);
		else printf("BIGGESTn");
	}
	return 0;
}

 

 

POJ1833:排列——全排列问题

Description

题目描述:
大家知道,给出正整数n,则1到n这n个数可以构成n!种排列,把这些排列按照从小到大的顺序(字典顺序)列出,如n=3时,列出1 2 3,1 3 2,2 1 3,2 3 1,3 1 2,3 2 1六个排列。

任务描述:
给出某个排列,求出这个排列的下k个排列,如果遇到最后一个排列,则下1排列为第1个排列,即排列1 2 3…n。
比如:n = 3,k=2 给出排列2 3 1,则它的下1个排列为3 1 2,下2个排列为3 2 1,因此答案为3 2 1。
Input

第一行是一个正整数m,表示测试数据的个数,下面是m组测试数据,每组测试数据第一行是2个正整数n( 1 <= n < 1024 )和k(1<=k<=64),第二行有n个正整数,是1,2 … n的一个排列。
Output

对于每组输入数据,输出一行,n个数,中间用空格隔开,表示输入排列的下k个排列。
Sample Input

3
3 1
2 3 1
3 1
3 2 1
10 2
1 2 3 4 5 6 7 8 9 10
Sample Output

3 1 2
1 2 3
1 2 3 4 5 6 7 9 8 10

一道全排列问题。用next_permutation轻松AC。注意头文件是algorithm即可。


#include
#include
using namespace std;
int cache[2000];
int main()
{
	int N;
	scanf("%d", &N);
	while (N--)
	{
		int n, k, i;
		scanf("%d%d",&n,&k);
		for (i = 0; i < n; i++)
			scanf("%d", &cache[i]);
		for (i = 0; i < k; i++)
			next_permutation(cache, cache + n);
		printf("%d", cache[0]);
		for (i = 1; i < n; i++)
			printf(" %d", cache[i]);
		printf("n");
	}
	return 0;
}

 

POJ1146:ID Codes——全排列问题

Description

It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In order to exercise greater control over its citizens and thereby to counter a chronic breakdown in law and order, the Government decides on a radical measure–all citizens are to have a tiny microcomputer surgically implanted in their left wrists. This computer will contains all sorts of personal information as well as a transmitter which will allow people’s movements to be logged and monitored by a central computer. (A desirable side effect of this process is that it will shorten the dole queue for plastic surgeons.)

An essential component of each computer will be a unique identification code, consisting of up to 50 characters drawn from the 26 lower case letters. The set of characters for any given code is chosen somewhat haphazardly. The complicated way in which the code is imprinted into the chip makes it much easier for the manufacturer to produce codes which are rearrangements of other codes than to produce new codes with a different selection of letters. Thus, once a set of letters has been chosen all possible codes derivable from it are used before changing the set.

For example, suppose it is decided that a code will contain exactly 3 occurrences of `a’, 2 of `b’ and 1 of `c’, then three of the allowable 60 codes under these conditions are:
abaabc

abaacb

ababac

These three codes are listed from top to bottom in alphabetic order. Among all codes generated with this set of characters, these codes appear consecutively in this order.

Write a program to assist in the issuing of these identification codes. Your program will accept a sequence of no more than 50 lower case letters (which may contain repeated characters) and print the successor code if one exists or the message `No Successor’ if the given code is the last in the sequence for that set of characters.
Input

Input will consist of a series of lines each containing a string representing a code. The entire file will be terminated by a line consisting of a single #.
Output

Output will consist of one line for each code read containing the successor code or the words ‘No Successor’.
Sample Input

abaacb
cbbaa
#
Sample Output

ababac
No Successor


 

C++中STL的next_permutation类问题,查找排序中的下一序列。

简单AC代码:

#include
#include
using namespace std;
int main()
{
	int lenth;
	char s[51];
	while (scanf("%s", s) != EOF)
	{
		if (s[0] == '#')
			break;
		lenth = strlen(s);
		if (next_permutation(s, s + lenth))
			printf("%sn", s);
		else
			printf("No Successorn");
	}
	return 0;
}

 

嘘,老大哥在盯着你…………

HDOJ1008:Elevator——简单数学题

Problem Description
The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.

For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.

Input
There are multiple test cases. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100. A test case with N = 0 denotes the end of input. This test case is not to be processed.

Output
Print the total time on a single line for each test case.

Sample Input
1 2
3 2 3 1
0

Sample Output
17
41

Author
ZHENG, Jianqiang

别人问的一道题目,没什么好说的。要注意即便是相同楼层也要停5s才行。

样例:

3 1 1 1

输出:

21

 
代码:

#include
int main()
{
	int n;
	while (~scanf("%d", &n)&&n!=0)
	{
		int f, fi,j, sum;
		f = sum = 0;
		for (j = 0; j f)
			{
				sum += 4 * (fi - f);
			}
			//if (j!=n-1)注意最后也要停五秒
				sum += 5;
		}
		printf("%dn", sum);
	}
	return 0;
}

临近期末,心浮气躁。来点水题降降火~

POJ3617:Best Cow Line——字典序最小问题

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


题目大意是给定一个长度为N的字符串S,要求构造一个字符串T。T的默认状态为空。然后随机将S的头部(或尾部)删除一个字符,加到T的尾部。

要求构造一个字典序尽可能小的字符串T。

思路:

  1. 按照字典序比较S和反转后的字符串S’
  2. 如果S较小,就从S开头取出字符,放到T的末尾
  3. 如果S’较小,就从S末位取出字符,放到T的末尾
  4. 如果相同,则比较(S+1)和(S’-1)两字符的大小;相同则继续。

字典序+贪心问题。

出现PE问题:每行最多80个字符

 

#include
#include
#include
int N,i;
const int MAX_N = 2000;
char S[MAX_N + 1];

int main()
{
	while (~scanf("%d", &N))
	{
		for (i = 0; i < N; i++)
		{
			getchar();
			scanf("%c", &S[i]);
		}
		int a = 0, b = N - 1, cnt;
		cnt = 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--]);
			cnt++;
			if (cnt % 80 == 0)
				putchar('n');
		}
	}
	return 0;
}

 

区间调度问题

题目描述:

有n项工作,每项工作分别在si时间开始,ti时间结束。对于每项工作,你都可以选择参加或不参加,但选择了参加某项工作就必须至始至终参加全程参与,即参与工作的时间段不能有重叠(即使开始的瞬间和结束的瞬间重叠也是不允许的)。

你的目标是参与尽可能多的工作,那么你最多能参与多少项工作呢?

限制条件:

1<=n<=100000

1<=si<=ti,=10^9


这个问题首先想到的就是贪心算法。但设计算法时容易出现错误。

比较容易想到的算法如:

  • 在可选工作中,每次选取最先开始的工作
  • 在可选工作中,每次选取用时最短的工作
  • 在可选工作中,每次选取与最少可选工作有重叠的工作
  • 在可选工作中,每次选取结束时间最早的工作

而很显然,前三项算法都不具有普适性(例如:一项开头早,持续时间长的工作和两项开头晚的工作)。

关于最后一种算法的解释:

结束时间早,对应的可选工作就越多。当然也可以用归纳法来证明。

代码实现:

 

#include 
#include 
#include 
#include 
using namespace std;

const int N = 5;
int s[N]={1,2,4,6,8};
int t[N]={3,5,7,9,10};

int solve()
{
pair itv[N];
for(int i = 0; i 
	

栈是支持push和pop的数据结构。

push是在栈的顶端放入一组数据;pop是在栈的顶端取出一组数据。

Last in first out(LIFO):最后进入栈的一组数据会被最先取出.


 

样例:

 

#include
#include
using namespace std;
int main()
{
	stack s;//声明int类型数据存储的栈
	s.push(1);//『』→『1』
	s.push(2);//『1』→『1,2』
	s.push(3);//『1,2』→『1,2,3』
	printf("%dn",s.top());//3
	s.pop();//栈顶移除3,下同
	printf("%dn",s.top());
	s.pop();
	printf("%dn",s.pop());
	s.pop();//『1』→『』
	return 0;
}

返回栈顶元素:

// test_stack.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include 
#include 
#include 
#include 

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	stack mystack;
	mystack.push(10);
	mystack.push(20);
	mystack.top()-=5;
	cout 
	

HDOJ5062:Beautiful Palindrome Number

Problem Description
A positive integer x can represent as (a1a2…akak…a2a1)10 or (a1a2…ak−1akak−1…a2a1)10 of a 10-based notational system, we always call x is a Palindrome Number. If it satisfies 0<a1<a2<…<ak≤9, we call x is a Beautiful Palindrome Number.
Now, we want to know how many Beautiful Palindrome Numbers are between 1 and 10N.

Input
The first line in the input file is an integer T(1≤T≤7), indicating the number of test cases.
Then T lines follow, each line represent an integer N(0≤N≤6).

Output
For each test case, output the number of Beautiful Palindrome Number.

Sample Input
2
1
6

Sample Output
9
258


要求判断范围内有多少回文串。由于范围小,可以直接打表计算。

#include
int main()
{
	int a[7]={1,9,18,54,90,174,258},t,n;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		printf("%dn",a[n]);
	}
return 0;
}

 

HDOJ2108:Shape of HDU

Problem Description
话说上回讲到海东集团推选老总的事情,最终的结果是XHD以微弱优势当选,从此以后,“徐队”的称呼逐渐被“徐总”所取代,海东集团(HDU)也算是名副其实了。
创业是需要地盘的,HDU向钱江肉丝高新技术开发区申请一块用地,很快得到了批复,据说这是因为他们公司研发的“海东牌”老鼠药科技含量很高,预期将占全球一半以上的市场。政府划拨的这块用地是一个多边形,为了描述它,我们用逆时针方向的顶点序列来表示,我们很想了解这块地的基本情况,现在请你编程判断HDU的用地是凸多边形还是凹多边形呢?

 

Input
输入包含多组测试数据,每组数据占2行,首先一行是一个整数n,表示多边形顶点的个数,然后一行是2×n个整数,表示逆时针顺序的n个顶点的坐标(xi,yi),n为0的时候结束输入。

 

Output
对于每个测试实例,如果地块的形状为凸多边形,请输出“convex”,否则输出”concave”,每个实例的输出占一行。

 

Sample Input
4
0 0 1 0 1 1 0 1
0

 

Sample Output
convex

 

采用的方法是三角函数求内角度数,判断角是否超过180.

问题:

sqrt函数中形参必须为double,但double不能用^2进行乘方,降低了代码可读性。

忘记判断最后一点与初始两点的角度。

还是WA:

#include
#include
#include
int main()
{
	int  flag,n,ni;
	double a, b, c,cc,arc;
	double num[2][10000];
	while(~scanf("%d", &n)&&n!=0)
	{
		memset(num, 0, sizeof(num));
		flag = 0;
		for (ni = 0; ni < n; ni++)
		{
			scanf("%lf%lf", &num[0][ni], &num[1][ni]);
		}
		for (ni = 2; ni < n; ni++)
		{
			a = sqrt((num[0][ni - 2] - num[0][ni - 1])*(num[0][ni - 2] - num[0][ni - 1]) + (num[1][ni - 2] - num[1][ni - 1])*(num[1][ni - 2] - num[1][ni - 1]));
			b = sqrt((num[0][ni - 1] - num[0][ni])*(num[0][ni - 1] - num[0][ni]) + (num[1][ni - 1] - num[1][ni])*(num[1][ni - 1] - num[1][ni]));
			c = sqrt((num[0][ni - 2] - num[0][ni])*(num[0][ni - 2] - num[0][ni]) + (num[1][ni - 2] - num[1][ni])*(num[1][ni - 2] - num[1][ni]));
			cc = (a*a + b*b - c*c) / (2 * a*b);
			arc = acos(cc) * 180 / 3.1415926;
			if (arc>(double)180||arc<(double)0)
			{
				flag = 1;
			}
		}
		a = sqrt((num[0][ni] - num[0][0])*(num[0][ni] - num[0][0]) + (num[1][ni] - num[1][0])*(num[1][ni] - num[1][0]));
		b = sqrt((num[0][0] - num[0][1])*(num[0][0] - num[0][1]) + (num[1][0] - num[1][1])*(num[1][0] - num[1][1]));
		c = sqrt((num[0][ni] - num[0][1])*(num[0][ni] - num[0][1]) + (num[1][ni] - num[1][1])*(num[1][ni] - num[1][1]));
		cc = (a*a + b*b - c*c) / (2 * a*b);
		arc = acos(cc) * 180 / 3.1415926;
		if (arc>(double)180 || arc<(double)0)
		{
			flag = 1;
		}
		if (flag = 0)
		{
			printf("convexn");
		}
		if (flag = 1)
		{
			printf("concaven");
		}
	}
	return 0;
}

 


更新下AC代码:

思路是判断相邻两条边的斜率,如果第二条边斜率小于第一条边,则为凹。

 

#include
#include
struct xy
{
    int x;
    int y;
};
typedef struct xy xy;
int main()
{
    xy num[10001],d,z;
    int a, b, c, cc, n, ni;
    int flag;
    while (~scanf("%d",&n)&&n!=0)
    {
        flag = 0;
        for (ni = 0; ni