博文

C++ String 类(2007-09-07 23:16:00)

摘要:///////////////////              该文系网上转载 之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必担心内存是否足够、字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下(甚至是100%)的需要。我们可以用 = 进行赋值操作,== 进行比较,+ 做串联(是不是很简单?)。我们尽可以把它看成是C++的基本数据类型。
    好了,进入正题………
首先,为了在我们的程序中使用string类型,我们必须包含头文件 <string>。如下:
    #include <string> //注意这里不是string.h string.h是C字符串头文件

1.声明一个C++字符串
声明一个字符串变量很简单:
    string Str;
这样我们就声明了一个字符串变量,但既然是一个类,就有构造函数和析构函数。上面的声明没有传入参数,所以就直接使用了string的默认的构造函数,这个函数所作的就是把Str初始化为一个空字符串。String类的构造函数和析构函数如下:
a)    string s;  //生成一个空字符串s
b)    string s(str) //拷贝构造函数 生成str的复制品
c)    string s(str,stridx) //将字符串str内“始于位置stridx”的部分当作字符串的初值
d)    string s(str,stridx,strlen) //将字符串str内“始于stridx且长度顶多strlen”的部分作为字符串的初值
e)    string s(cstr)......

阅读全文(2618) | 评论:1

通配符匹配串(2007-09-07 22:48:00)

摘要: Wildcard Characters Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB Problem description In computer (software) technology, a wildcard character can be used to substitute for any other character or characters in a string.
The asterisk (*) usually substitutes as a wildcard character for any zero or more characters, and the question mark (?) usually substitutes as a wildcard character for any one character, as in the CP/M, DOS, Microsoft Windows and POSIX (Unix) shells. (In Unix this is referred to as glob expansion.)

For example: a string "wildcard" can be matched by expression "*", "**", "????????", "?ildcar?", "*card", "?ild*d", etc.


Input The first line of input contains a non-negtive integer t(1 <= t <= 1000), then followed t lines, each contains a single word(composed by alphabet characters, digits or underlines "_", and no longer than 10 characters), the next line contains a non-negtive integer n(1......

阅读全文(5043) | 评论:0

字母表示数(2007-09-07 13:08:00)

摘要: String’s Puzzle Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB Total submit users: 11, Accepted users: 11 Problem 10783 : No special judgement Problem description Zhu Ming and Small Li find a strange stone tablet, which has some capitals printed on it. Capitals in every string are different with each other, and every capital string is followed by a number. Because of their supernormal intelligence, Zhu Ming and Small Li find the number following the string is numbered in the order of dictionary sequence from little to big (the original number is 0).For example, when the length of capital string is 3, number following the string will be: ABC 0 ABD 1 …… ZYX 15599 To better study this puzzle, please make a program to given a n-length string (1<=n<=26), output the number of the string.

Input There are several test cases. Each data is made up of two lines. The first line is number n (1<=n<=26) The second line is a ......

阅读全文(2911) | 评论:0

缩写匹配(2007-09-06 12:54:00)

摘要: Campus Buildings Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB Problem description Most campuses have a lot of named buildings, and as the names tend to be rather long, the class schedules have abbreviations. At USC, it seems that the abbreviations have become used so widely that most people don't even remember the full name of the building. SAL, PHE, OHE. To a newcomer, this can be quite confusing. So perhaps, we ought to write a little program that finds out which building could be meant by abbreviations.

Here's how we'll do it: you will be given a list of building names, and a building abbreviation, such as SAL or FRSSC. The abbreviation matches the building name if all of its letters appear, in this order, in the building name (no letter can be matched twice). So, for instance, SAL matches "SALvatori", or "Student Aerospace Laboratory", or "univerSity of southern cALifornia". It does not match "angeles", as the letters are ......

阅读全文(2858) | 评论:0

累加回文(2007-09-06 11:35:00)

摘要: Eventual Palindromes Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB Problem description Consider the decimal number 193. Reverse its digits and add the result to the starting number. The answer is 193 + 391 = 584. Repeat the process, 584 + 485 = 1,069. Continue the process until a palindrome is formed (a number that is the same when read in the forward or reverse directions). In this example, the palindrome is 233332, formed after eight applications of the “reverse and add” rule. All numbers are thought to yield palindromes eventually, although there is no proof of this, and, for some numbers, no palindrome has ever been found. Your task is to determine, for each input number, how many applications of the “reverse and add” rule are necessary to form a palindrome, and to find the palindrome.

Assume that no input number will be larger than 10,000. You can also assume that no palindrome will be larger than 4,668,731,596,684,224,866......

阅读全文(2917) | 评论:0

求和最大的子矩阵(2007-09-06 10:40:00)

摘要: Maximal Sub-rectangle Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB Problem description Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the largest sum. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle. A sub-rectangle is any contiguous sub-array of size 1 × 1 or greater located within the whole array. As an example, the maximal sub-rectangle of the array:

0 -2 -7 0 9 2 -6 2 -4 1 -4 1 -1 8 0 -2is in the lower-left-hand corner:
9 2 -4 1 -1 8
and has the sum of 15.

Input The input consists of an array of N × N integers. The input begins with a single positive integer N on a line by itself indicating the size of the square two dimensional array. This is followed by N2 integers separated by white-space (newlines and spaces). These N2 integers m......

阅读全文(4509) | 评论:1

走马(2007-09-06 09:52:00)

摘要: Playing Chess Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB Total submit users: 18, Accepted users: 18 Problem 10763 : No special judgement Problem description yiyi and birdman like to play chess.They sometimes play chess in QQ game. They discovered that the game cost a lot of time. They have lots of things to do, for instance doing homework, programming.So they come up with a good game, this game is played on the chessboard too, Is a 4 × 3 Lattice. However, only a pawn - horse. The horse on the chessboard of arbitrary position ,The pawn returns to the original location of a number of different routes ,(if a position has been arrived then should not go again and the horse takes ‘日’ route), They have to give a total number of different routes and the faster is the winner. Because they are very good at programming , Through programming they can easily give the answer. This new game is not a challenge to them ,But it maybe a challenge to......

阅读全文(2856) | 评论:0

求 n 个子序列的最大和(2007-09-05 22:08:00)

摘要: Find the max Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB Problem description Yiyiyi4321 is trying to solve the problem 10113 in acm.hnu.cn, and he found that in order to solve this problem he must learn to solve the problem below :
Give you a sequence of integers, find K subsequences of the sequence and make their sum Maximized.For example, given a sequence 3, 4, -7, 3, 5, -2, 3, find 2 subsequences from the sequence, you could take 3,4 | 3,5,-2,3, their sum is 16. find 3 subsequences from the sequence, we can take 3,4 | 3,5 | 3 ,whose sum is 18.


Input There are two integers, separated by single spaces, in the first line : number of integers n (1 <= n <= 1000), number of subsequences K(0 <= K <= n).The consecutive n lines are the integers.


Output Your program should write one integer, the max sum of the K subsequence.

Sample Input 7 2 3 4 -7 3 5 -2 3 Sam......

阅读全文(2947) | 评论:0

求 n  个数的最小公倍数(2007-09-05 18:58:00)

摘要: Least Common Multiple Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB Problem description In arithmetic and number theory the least common multiple or lowest common multiple (lcm) or smallest common multiple of two integers a and b is the smallest positive integer that is a multiple of both a and b. If there is no such positive integer, e.g., if a = 0 or b = 0, then lcm(a, b) is defined to be zero.
For example, the least common multiple of the numbers 4 and 6 is 12.


Input The input contains several cases. Each case has one line containing a non-negative integer N (N<=100), then followed N non-negative integers in the range of 0 and 2147483647. A ZERO terminates the input, which should not be processed.

Output For each test case, output the least common multiple of the N integers in seperate lines. It is guaranteed that the lcm would not exceed 231 - 1.

Sample Input 2 12 18 5 3......

阅读全文(5169) | 评论:0

排队买票(2007-09-05 18:38:00)

摘要: Buy tickets Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB Problem description There are N people who want to buy some tickets, and there are M places to serve them. These people are numbered from 1 to N , and person i will be served in Ti minites. If N > M, some people will have to wait. Now we want to rearrange the sequence of these people so that the sum of the waiting time of all the people will be minimized, and your task is to calculate the minimum waiting time.

Input The first line is two integers N(1 <= N <= 1000) and M(1 <= M <= 100), which is the number of the people and the number of places to serve them. The following n lines is the time which the N people need to be served.

Output Only one integer, the minimum waiting time.

Sample Input 5 3 3 4 2 8 4 Sample Output 5 //// 第一行输入 n m  ,n 表示需要买票的总人数,m 表示提供服务的窗口数目,接下来的 n 行每行一个整数表示第 i (1<=i<=n) 个......

阅读全文(3253) | 评论:0