![](https://img-blog.csdnimg.cn/20201014180756916.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法
文章平均质量分 59
四郎别认输
这个作者很懒,什么都没留下…
展开
-
kmp算法
#include <iostream>#include <string>#include <vector>using namespace std;int nexts[100];int Index_KMP(string s,string t,int pos){ int sl=s.size(); int tl=t.size(); int i=pos; int j=0; while(i<sl&&j.原创 2021-03-12 17:54:27 · 215 阅读 · 0 评论 -
1039 Course List for Student
Zhejiang University has 40000 students and provides 2500 courses. Now given the student name lists of all the courses, you are supposed to output the registered course list for each student who comes for a query.Input Specification:Each input file cont原创 2021-02-07 23:14:37 · 199 阅读 · 0 评论 -
1024 Palindromic Number
A number that will be the same when it is written forwards or backwards is known as aPalindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers.Non-palindromic numbers can be paired with palindromi.原创 2021-02-03 21:33:52 · 207 阅读 · 0 评论 -
1023 Have Fun with Numbers
Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a diffe原创 2021-02-03 20:30:06 · 121 阅读 · 0 评论 -
1059 Prime Factors
Given any positive integerN, you are supposed to find all of its prime factors, and write them in the formatN=p1k1×p2k2×⋯×pmkm.Input Specification:Each input file contains one test case which gives a positive integerNin...原创 2021-02-02 13:10:26 · 70 阅读 · 0 评论 -
算法笔记
目录基础数学问题质因子分解例题基础数学问题质因子分解质因子分解是指将一个正整数n写成一个或多个质数的乘积的形式。思路:可以先把素数表打印出来定义一个结构体数组factor用来存放质因子struct factor{ int x,cnt; //x为质因子,cnt为其个数}fac[10];思路枚举1到sqrt(n)的所有质因子p,判断p是否是n的因子,是则加入,不是跳过 如果最后的n不等于1,说明有一个大于sqrt(n)的质因子,也要加入例题1096原创 2021-02-02 11:52:48 · 126 阅读 · 0 评论 -
1096 Consecutive Factors
Among all the factors of a positive integer N, there may exist several consecutive numbers. For example, 630 can be factored as 3×5×6×7, where 5, 6, and 7 are the three consecutive numbers. Now given any positive N, you are supposed to find the maximum num原创 2021-02-02 11:51:05 · 88 阅读 · 0 评论 -
1015 Reversible Primes (20分)
Areversible primein any number system is a prime whose "reverse" in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime.Now given any two positive integersN(<105)...原创 2021-01-31 17:01:15 · 71 阅读 · 0 评论 -
1078 Hashing (25分)
The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to beH(key)=key%TSizewhereTSizeis the maximum size of the hash table. Qua...原创 2021-01-31 15:17:48 · 72 阅读 · 0 评论 -
1081 Rational Sum (20分)
GivenNrational numbers in the formnumerator/denominator, you are supposed to calculate their sum.Input Specification:Each input file contains one test case. Each case starts with a positive integerN(≤100), followed in the next lineNrational numb...原创 2021-01-26 20:47:40 · 83 阅读 · 1 评论 -
1008 Elevator (20分)
The highest building in our city has only one elevator. A request list is made up withNpositive 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..原创 2021-01-25 19:06:55 · 70 阅读 · 0 评论 -
1101 Quick Sort (25分)
There is a classical process namedpartitionin the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements less than the pivot are moved to its left and those larger than the pivot to its right. GivenN...原创 2021-01-25 09:20:41 · 74 阅读 · 0 评论 -
93 Count PAT‘s (25分)
The stringAPPAPTcontains twoPAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th characters.Now given any string, you are supposed to tell the number of...原创 2021-01-24 21:52:38 · 94 阅读 · 1 评论 -
1029 Median (25分)
1029Median(25分)Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1 = { 11, 12, 13, 14 } is 12, and the median of S2 = { 9, 10, 15, 16, 17 } is 15. The median of two sequences is d..原创 2021-01-15 21:52:14 · 109 阅读 · 0 评论 -
1089 Insert or Merge (25分)
According to Wikipedia:Insertion sortiterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, a.原创 2021-01-15 21:30:16 · 82 阅读 · 0 评论 -
1048 Find Coins (25分)
Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: for eac原创 2021-01-14 16:54:08 · 89 阅读 · 0 评论 -
快速幂模板
递归版本typedef long long ll;//求a^b%m得快速幂的递归写法ll binaryPow(ll a,ll b,ll m){ if(b==0) return 1; //如果b为0,那么a^0=1; //b为奇数,转为b-1 if(b&1) //替换b%2==1,位与操作速度更快 return a*binaryPow(a,b-1)%m; else{ //b为偶数,转为b/2 ll m原创 2021-01-14 11:27:14 · 82 阅读 · 0 评论 -
解决寻找有序序列第一个满足某条件的元素的位置的固定模板(二分思想)
//解决“寻找有序序列第一个满足某条件的元素的位置”问题的固定模板//二分区间为[left,right],初值必须能覆盖解的所有可能取值int solve(int left,int right){ int mid; while(left<right) { mid=(left+right)/2; if(条件成立) //第一个满足该条件的元素的位置<=mid { right=mid;//搜左区..原创 2021-01-14 10:08:04 · 101 阅读 · 0 评论 -
1067 Sort with Swap(0, i) (25分)
Given any permutation of the numbers {0, 1, 2,...,N−1}, it is easy to sort them in increasing order. But what ifSwap(0, *)is the ONLY operation that is allowed to use? For example, to sort {4, 0, 2, 1, 3} we may apply the swap operations in the followin...原创 2021-01-13 16:01:55 · 92 阅读 · 0 评论 -
1037 Magic Coupon (25分)
The magic shop in Mars is offering some magic coupons. Each coupon has an integerNprinted on it, meaning that when you use this coupon with a product, you may getNtimes the value of that product back! What is more, the shop also offers some bonus produ...原创 2021-01-13 14:28:11 · 101 阅读 · 0 评论 -
1005 Spell It Right (20分)
Given a non-negative integerN, your task is to compute the sum of all the digits ofN, and output every digit of the sum in English.Input Specification:Each input file contains one test case. Each case occupies one line which contains anN(≤10100)...原创 2021-01-10 21:12:20 · 100 阅读 · 1 评论 -
1025 PAT Ranking (25分)
Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your原创 2021-01-10 20:53:58 · 64 阅读 · 0 评论 -
算法分析与设计(王秋芬)(1)——贪心法
贪心法贪心法的基本思想每个阶段面临选择时,贪心法都做出对眼前情况的最优解,不考虑后续影响。 每个阶段的决策一旦做出,不可以更改,不能回溯 贪心法是根据贪心策略来逐步构造问题的解,策略不同结果不同 贪心法具有高效性和不稳定性,它可以很快得到解,但不一定是最优解。贪心算法的好坏关键在于贪心策略的选择贪心法的基本要素(适合的问题)最优子结构性质——当一个问题的最优解一定包含其子问题的最优解时,则该问题具有最优子结构性质 贪心选择性质——所求问题的整体最优解可以通过一系列局部最优的选择获得原创 2020-12-17 20:30:45 · 723 阅读 · 0 评论 -
1004 Counting Leaves (30分)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.Input Specification:Each input file contains one test case. Each case starts with a line containing0<N<100, the number of nod.原创 2020-12-11 09:56:58 · 73 阅读 · 0 评论 -
1003 Emergency (25分)——迪杰斯特拉模板题
As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked原创 2020-12-10 09:46:55 · 186 阅读 · 0 评论 -
最短路算法——Dijkstra以及它的优化
示例图代码#include <iostream>#include <math.h>#include <algorithm>#include <stdlib.h>#include <stdio.h>#include <iomanip>#include <string.h>#include <vector>#include <set>#include <map>原创 2020-12-09 20:55:59 · 154 阅读 · 0 评论 -
算法分析与设计(王秋芬)(1)——算法及基础知识
算法及基础知识算法的定义与特性定义:指的是对特定问题求解步骤的一种描述,是若干指令的有穷指令特性:输入——有零个或多个输入,来源于外界提供或自己产生 输出——有一个或多个输出 确定性——组成算法的每条指令必须有确定的含义,无歧义 有限性——算法中每条指令的执行次数都是有限的,执行每条指令的时间也是有限的 可行性——一个算法是可行的,即算法中描述的操作都可以通过已经实现的基本运算执行有限次后实现算法的描述方式:常用的描述方式有自然语言、图形、程序设计语言、伪代码等算法与程序的区别原创 2020-12-09 16:43:40 · 1302 阅读 · 0 评论 -
1002 A+B for Polynomials (25分)
This time, you are supposed to findA+BwhereAandBare two polynomials.Input Specification:Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:KN1aN1N2aN2...原创 2020-12-08 08:06:53 · 152 阅读 · 0 评论 -
1075 链表元素分类 (25分)
定一个单链表,请编写程序将链表元素进行分类排列,使得所有负值元素都排在非负值元素的前面,而 [0, K] 区间内的元素都排在大于 K 的元素前面。但每一类内部元素的顺序是不能改变的。例如:给定链表为 18→7→-4→0→5→-6→10→11→-2,K 为 10,则输出应该为 -4→-6→-2→7→0→5→10→18→11。输入格式:每个输入包含一个测试用例。每个测试用例第 1 行给出:第 1 个结点的地址;结点总个数,即正整数N (≤105);以及正整数K (≤103)。结点的地址是 5原创 2020-12-04 16:57:23 · 105 阅读 · 0 评论 -
1072 开学寄语 (20分)
下图是上海某校的新学期开学寄语:天将降大任于斯人也,必先删其微博,卸其 QQ,封其电脑,夺其手机,收其 ipad,断其 wifi,使其百无聊赖,然后,净面、理发、整衣,然后思过、读书、锻炼、明智、开悟、精进。而后必成大器也!本题要求你写个程序帮助这所学校的老师检查所有学生的物品,以助其成大器。输入格式:输入第一行给出两个正整数 N(≤1000)和 M(≤6),分别是学生人数和需要被查缴的物品种类数。第二行给出 M 个需要被查缴的物品编号,其中编号为 4 位数字。随后 N 行,每行给出一位..原创 2020-12-04 09:44:46 · 179 阅读 · 0 评论 -
1064 朋友数 (20分)
如果两个整数各位数字的和是一样的,则被称为是“朋友数”,而那个公共的和就是它们的“朋友证号”。例如 123 和 51 就是朋友数,因为 1+2+3 = 5+1 = 6,而 6 就是它们的朋友证号。给定一些整数,要求你统计一下它们中有多少个不同的朋友证号。输入格式:输入第一行给出正整数 N。随后一行给出 N 个正整数,数字间以空格分隔。题目保证所有数字小于104。输出格式:首先第一行输出给定数字中不同的朋友证号的个数;随后一行按递增顺序输出这些朋友证号,数字间隔一个空格,且行末不得有多.原创 2020-12-02 20:44:18 · 149 阅读 · 0 评论 -
1065 单身狗 (25分)
“单身狗”是中文对于单身人士的一种爱称。本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱。输入格式:输入第一行给出一个正整数 N(≤50 000),是已知夫妻/伴侣的对数;随后 N 行,每行给出一对夫妻/伴侣——为方便起见,每人对应一个 ID 号,为 5 位数字(从 00000 到 99999),ID 间以空格分隔;之后给出一个正整数 M(≤10 000),为参加派对的总人数;随后一行给出这 M 位客人的 ID,以空格分隔。题目保证无人重婚或脚踩两条船。输出格式:首先第一行输..原创 2020-12-02 20:20:25 · 182 阅读 · 0 评论 -
动态规划——最小编辑距离(算法实验报告)
最小编辑距离编辑距离是指两个字串之间,由一个转成另一个所需的最少编辑操作次数。许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字符。。求出两个字符串的最小编辑距离思路:将问题细化到每一步,判断当前进行哪一类操作可以获得当前的最小的次数,以此类推获得最终的最小编辑距离代码思路:1.设字符串长度为n,m,先构造一个n*m的二维数组2.在这个二维数组的[0][0]到[n][0]赋值0到n,[0][0]到[0][m]赋值0到m3.定义向下为删除,向右为添加,向右下原创 2020-12-02 19:16:59 · 3456 阅读 · 1 评论 -
快速排序——算法实验报告
快排实验报告快排基本思想:使用分治的思想,每次排序将待排序列分成两部分,其中一部分记录的关键字均比另一部分记录的关键字小。之后分别对这两部分记录继续进行排序,以达到整个序列有序的目的。快排的编程思路1.找定一个关键数,然后定义一个左指针和一个右指针。2.将左指针指向的数中比关键数大的换到右边,将右指针指向的数比关键数小的换到左边,直到左右指针相遇。3.这事保证左边的数都小于关键数,右边的数都大于关键数,此时再对两个子序列递归地进行相同操作,最终获得有序列代码#include原创 2020-12-02 15:24:07 · 3638 阅读 · 0 评论 -
C++的getline和cin的区别
geline可以接收空格cin以空格作为输入的结束代码示例#include <iostream>using namespace std;int c[128];int main(){ string s1,s2; getline(cin,s1); cin>>s2; cout<<s1<<endl; cout<<s2<<endl; return 0;}运行结果原创 2020-11-25 19:50:08 · 471 阅读 · 0 评论 -
1033 旧键盘打字 (20分)
旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及坏掉的那些键,打出的结果文字会是怎样?输入格式:输入在 2 行中分别给出坏掉的那些键、以及应该输入的文字。其中对应英文字母的坏键以大写给出;每段文字是不超过105个字符的串。可用的字符包括字母 [a-z,A-Z]、数字0-9、以及下划线_(代表空格)、,、.、-、+(代表上档键)。题目保证第 2 行输入的文字串非空。注意:如果上档键坏掉了,那么大写的英文字母无法被打出。输出格式:...转载 2020-11-25 19:36:18 · 378 阅读 · 0 评论 -
1029 旧键盘
旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及实际被输入的文字,请你列出肯定坏掉的那些键。输入格式:输入在 2 行中分别给出应该输入的文字、以及实际被输入的文字。每段文字是不超过 80 个字符的串,由字母 A-Z(包括大、小写)、数字 0-9、以及下划线_(代表空格)组成。题目保证 2 个字符串均非空。输出格式:按照发现顺序,在一行中输出坏掉的键。其中英文字母只输出大写,每个坏键只输出一次。题目保证至少有 1 个坏键。输入样例:.原创 2020-11-23 16:05:40 · 99 阅读 · 0 评论 -
1028 人口普查
某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过 200 岁的老人,而今天是 2014 年 9 月 6 日,所以超过 200 岁的生日和未出生的生日都是不合理的,应该被过滤掉。输入格式:输入在第一行给出正整数N,取值在(0,105];随后N行,每行给出 1 个人的姓名(由不超过 5 个英文字母组成的字符串)、以及按yyyy/mm/dd(即年/月/日)格式给出的生日。题目保...原创 2020-11-23 15:29:30 · 81 阅读 · 0 评论 -
1026 程序运行时间
要获得一个 C 语言程序的运行时间,常用的方法是调用头文件 time.h,其中提供了 clock() 函数,可以捕捉从程序开始运行到 clock() 被调用时所耗费的时间。这个时间单位是 clock tick,即“时钟打点”。同时还有一个常数 CLK_TCK,给出了机器时钟每秒所走的时钟打点数。于是为了获得一个函数f的运行时间,我们只要在调用f之前先调用 clock(),获得一个时钟打点数 C1;在f执行完成后再调用 clock(),获得另一个时钟打点数 C2;两次获得的时钟打点数之差 (C2-...原创 2020-11-23 14:56:35 · 67 阅读 · 0 评论 -
1027 打印沙漏 (20分)
本题要求你写个程序把给定的符号打印成沙漏的形状。例如给定17个“*”,要求按下列格式打印***** *** * ********所谓“沙漏形状”,是指每行输出奇数个符号;各行符号中心对齐;相邻两行符号数差2;符号数先从大到小顺序递减到1,再从小到大顺序递增;首尾符号数相等。给定任意N个符号,不一定能正好组成一个沙漏。要求打印出的沙漏能用掉尽可能多的符号。输入格式:输入在一行给出1个正整数N(≤1000)和一个符号,中间以空格分隔。输出格式:首先打印出由给定符号组成的最原创 2020-11-23 11:03:40 · 174 阅读 · 0 评论