自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(145)
  • 收藏
  • 关注

原创 C++ std::mem_fn使用示例

#include #include #include #include #include #include #include #include using namespace std;using namespace std::placeholders;void hello(const string& s){ cout << s << endl;}struct

2016-09-13 11:52:04 4194

原创 普通素数筛法

给定一个数N,得到N以内的所有素数,可以利用素数筛法来做,代码如下:#include #include #include #include using namespace std;vector sievePrimes(int n){ vector result; vector flag(n+1, true); flag[0] = flag[1]

2016-09-12 23:30:21 586

原创 RIME输入法无重码自动上屏

配置文件中如:wubi86.custom.yaml speller下加上max_code_length: 4 # 最长4码auto_select: true # 顶字上屏auto_select_unique_candidate: true # 无重码自动上屏

2016-08-09 09:56:12 5149 1

原创 编译Emacs出现libgif.so.7: cannot open shared object file: No such file or directory

明明已经安装了libgif,还是并且 $ sudo find / -name "libgif.so.7" /usr/local/lib/libgif.so.7 解决办法:在编译安装完 libgif 后需要执行$ ldconfig完美解决!!!

2016-08-08 23:51:19 2453 1

原创 左旋转字符串m位

题目描述    定义字符串的左旋转操作:把字符串前面的若干个字符移动到字符串的尾部,如把字符串abcdefg左旋转3位得到字符串defgabc。请实现字符串左旋转的函数,要求对长度为n的字符串操作的时间复杂度为O(n),空间复杂度为O(1)。解法一:暴力移位    即每次移1位,移m次,空间复杂度为O(1),但时间复杂度达到了O(m*n);void leftshift1bit(

2016-04-04 14:04:44 544

原创 大数加法、减法、乘法、除法实现

〇、准备        这里,我们暂时只考虑正数的情况,负数请自行预处理,首先定义一些公用函数// num is positivevector toArray(const string& num){ vector arr; transform(num.rbegin(), num.rend(), back_inserter(arr), [](char c){ return

2016-03-26 17:26:49 1125

原创 fibonacci 数列的快速幂求法

一、经典解法        Fibonacci数列的经典解法,即递归解法,如下int fib(int n){ if(n <= 0) return 0; if(n == 1) return 1; return fib(n-1) + fib(n-2);}代码是十分简洁,但有一个问题,即重复求解子问题,复杂度以 n 的指数方式递增。二、实用解法

2016-03-25 14:19:14 2766

原创 1057. Stack (30)

用直方图的方式来动态维护中位数#include #include #include #include #include using namespace std;#define MAX_INTEGER 100005int hist[MAX_INTEGER];inline int medianPostion(int n){ return n % 2 ? (n + 1)

2016-03-20 13:04:18 283

原创 反转单链表

始终维护三个链表节点,即当前节点,前一个节点与下一个节点。#include #include #include #include #include using namespace std;struct Node{ int key; Node* next; Node(int k) : key(k), next(nullptr){}};Node* makeList

2016-03-08 17:44:37 365

原创 1103. Integer Factorization (30)

DFS加剪枝#include #include #include #include #include using namespace std;int n, k, p;vector> results;int maxSum = 0;int power(int num, int exp){ if(exp == 0) return 1; int val = power(

2016-03-08 10:47:43 354

原创 1102. Invert a Binary Tree (25)

唯一注意一点,就是根结点这里并未给出,需要自己根据输入判断出来。#include #include #include #include #include #include using namespace std;#define INF (~(1<<31))struct Node{ int id, left, right; Node(){} Node(int i

2016-03-07 20:51:05 663

原创 1101. Quick Sort (25)

开辟两个数组分别记录当前位置左边的最大值与右边的最小值#include #include #include #include #include using namespace std;#define INF (~(1<<31))int main(){ int n; cin >> n; vector arr(n), maxLeft(n), minRight(n);

2016-03-07 17:54:24 384

原创 1107. Social Clusters (30)

将具有相同爱好的人首先在图中建立联系,然后DFS,走的过程中记录当前 Cluster 的人数,然后尚有未访问的则继续访问下去。#include #include #include #include #include using namespace std;#define INF (~(1<<31))vector> hobbies(1005);vector> graph

2016-03-07 16:26:30 730

原创 1106. Lowest Price in Supply Chain (25)

DFS走一遍找最低高度的路径#include #include #include #include #include using namespace std;#define INF (~(1<<31))vector> nodes;vector results;void dfs(int root, int level, int& minLevel){ if(nod

2016-03-07 15:49:00 510

原创 1105. Spiral Matrix (25)

#include #include #include #include using namespace std;#define INF (~(1<<31))int main(){ int n; scanf("%d", &n); vector arr(n); for(int i = 0; i < n; ++i){ scanf("%d", &arr[i]); }

2016-03-06 22:22:43 303

原创 1104. Sum of Number Segments (20)

#include #include using namespace std;int main(){ int n; scanf("%d", &n); double sum = 0, num = 0; int times = 0; for(int i = 0; i < n; ++i){ scanf("%lf", &num); sum += num * (i+1)*(n-i

2016-03-06 21:45:25 350

原创 最长公共子序列,Longest-Common-Subsequence(LCS)

公共子序列有别于公共子串,子序列可以不连续,如 X = [abcbdab], Y = [bdcaba],则 bcba 为其一个公共最长子序列。同样考虑DP方案,假设 X = , Y = ,LCS(X, Y) 表示二者的公共最长子序列,则有如果 xm == yn,则 LCS(X, Y) = xm + LCS(Xm-1, Yn-1);如果 xm != yn,则 LCS(X, Y) = max

2016-03-06 14:26:10 400

原创 最长公共子串 Longest-Common-Substring(LCS)

给定两个字符串X,Y,求二者最长的公共连续子串,如 X = [abcdefg],Y = [bacdegf],二者的最长公共连续子串是 cde 长度为3,这里讨论 DP 方案。    考虑 m = X.length,n = Y.length 开辟一个大小为 m x n 的数组 d[m][n],使用 d[i][j] 表示以 X[i] 与 Y[j] 结尾的最长公共子串的长度,因为要求子串连续,所

2016-03-06 13:49:34 646

原创 1026. Table Tennis (30)

将排队的人按照到达的时间排序,然后开始轮询,会有四种情况:桌子是VIP,人是VIP,则直接分配桌子不是VIP,人也不是VIP,则直接分配桌子是VIP,人不是VIP,则往后找,是否存在一个VIP,且其到达时间早于该桌子的可用时间,找到则给VIP,当前的人继续等待,找不到这样的VIP,则把该桌子给当前用户桌子不是VIP,人是VIP,则检查桌子,是否还存在一张VIP桌子也空闲着,如果有这样一张V

2016-03-05 13:42:11 501

原创 1100. Mars Numbers (20)

考查进制转换#include #include using namespace std;string t1[] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};string t2[] = {"", "tam", "hel", "maa",

2016-03-04 11:32:44 373

原创 1099. Build A Binary Search Tree (30)

从根结点开始,每次统计根节点左边孩子节点的数目n,以此确定当前根节点的key值,应该是所有的key排序之后的数组的第n+1个值,递归构建即可。#include #include #include #include #include using namespace std;struct Node{ int key, left, right; Node(){} Node

2016-03-04 10:45:41 336

原创 1098. Insertion or Heap Sort (25)

先判断是否为插入排序,不是的话找出堆排序进行到第几次,然后再来一次。#include #include #include #include using namespace std;int main(){ int n; scanf("%d", &n); vector arr(n), part(n); for(int i = 0; i < n; ++i){ sca

2016-03-04 10:09:34 356

原创 1097. Deduplication on a Linked List (25)

先走一遍,找出所有有效的链表节点,然后按要求将节点分成两组,然后输出#include #include #include #include #include using namespace std;struct Node{ int add, key, next; Node(){} Node(int a, int k, int n) : add(a), key(k),

2016-03-03 22:54:03 323

原创 1096. Consecutive Factors (20)

一开始思路错了,参考了这里的解法。#include #include #include using namespace std;int main(){ int n; scanf("%d", &n); pair p; int len = 0; int r = sqrt(n); for(int i = 2; i <= r; ++i){ int j = i;

2016-03-03 22:08:04 232

原创 1095. Cars on Campus (30)

考查结构体排序,需要足够细心,大致思路是先将所有记录按车牌号排序,车牌号一样按时间排序,然后将合法的记录挑选出来。在查询的时候,维护当前已经查询到何处,下一次查询从当前位置开始。查询中,如果是进的,则把当前车辆数加1,出的则减1,同时更新车辆所停靠的时间,顺便选出最长停靠时间及相关车辆。#include #include #include #include #include #i

2016-03-03 21:24:39 314

原创 1094. The Largest Generation (25)

走一遍BFS,计算每一层的孩子数目,同时跟踪最大值#include #include #include #include using namespace std;vector> tree;int main(){ int n,m; scanf("%d%d", &n, &m); tree.resize(n+1, vector()); for(int i = 0; i

2016-03-03 17:22:35 340

原创 1093. Count PAT's (25)

线性走一遍,如果遇到P则把当前位置之前P的个数加1,遇到A则更新当前位置之前 PA 的个数,遇到 T 则统计PAT的个数#include #include using namespace std;int main(){ string s; cin >> s; const int N = 1000000007; long long sum = 0; int p = 0,

2016-03-03 17:08:21 235

原创 1092. To Buy or Not to Buy (20)

直接建表查询#include #include #include #include using namespace std;int main(){ string owner, eva; cin >> owner >> eva; unordered_map table; for(auto& c : owner){ if(table.find(c) == table

2016-03-03 16:32:14 413

原创 1091. Acute Stroke (30)

主要在于理解题意,读了三遍,竟然没懂啥意思,最后还看了Uncle_Sugar 的解释才明白,真是给这题跪了。实质上是三维空间走一遍DFS或者BFS,统计一下连通区域中1的个数,其中如果一个连通区域1的个数小于给定阈值则不算进去。#include #include #include #include using namespace std;int graph[1286][1

2016-03-03 16:17:02 334

原创 1090. Highest Price in Supply Chain (25)

DFS走一遍,找出最大深度,如果最大深度的叶结点不止一个,则统计数量。#include #include #include #include using namespace std;vector> chains;int maxLevel = 0;int maxNum = 0;void dfs(int src, int level){ if(chains[src].e

2016-03-03 14:57:28 673

原创 1089. Insert or Merge (25)

判断是否为插入排序较为简单,只需找到第一个违反升序的数,判断从该数开始,其后所有的数是否与原数组一样,是则采用的插入排序,否则采用的归并排序。对于归并排序,关键是找到当前几个元素一组,然后进行下一次归并即可。#include #include #include #include using namespace std;bool isequal(vector& arr, vec

2016-03-03 14:36:08 998 1

原创 1088. Rational Arithmetic (20)

给定分子分母能准确给出其标准形式,期间需要求最大公约数,注意负数还有加括号的处理#include #include #include using namespace std;long long gcd(long long a, long long b){ while(b){ long long r = a%b; a = b; b = r; } return

2016-03-03 08:48:09 555

原创 1087. All Roads Lead to Rome (30)

DFS, 走的过程,把符合条件的全记录下来#include #include #include #include using namespace std;#define MAX_CITY (205)#define INF (~(1<<31))vector> graph(MAX_CITY, vector(MAX_CITY, -1));vector happy(MAX_C

2016-03-02 23:12:04 1103 2

原创 1086. Tree Traversals Again (25)

所有的Push组成前序遍历,pop出来的顺序为中序遍历,转化为知道前序与中序,求后序遍历的问题#include #include #include #include using namespace std;struct Node{ int val; Node* left; Node* right; Node(int v) : val(v), left(nullptr

2016-03-02 22:02:27 300

原创 1085. Perfect Sequence (25)

排序后贪心#include #include #include #include using namespace std;int main(){ int n, p; scanf("%d%d", &n, &p); vector arr(n); for(int i = 0; i < n; ++i){ scanf("%lld", &arr[i]); } sort

2016-03-01 11:28:07 269

原创 1082. Read Number in Chinese (25)

万位 --> 千万位 可以与 个位 --> 千位 一样的流程处理#include #include #include using namespace std;char t1[][10] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};char t2[][10] = {"", "", "Shi", "Bai"

2016-03-01 10:13:35 268

原创 1080. Graduate Admission (30)

在处理超出学校名额的情况仍然有可能录取的情况时,为每个学校加入一个变量以指示最后一个录入同学的排名,当后来的同学与这个排名相同时则不再考虑是否名额不够。#include #include #include #include using namespace std;struct School{ int quota; int rank; // the last one's

2016-02-28 21:05:10 565

原创 1079. Total Sales of Supply Chain (25)

建立好树结构后,从根结点走一遍DFS,碰到 retailer 加上其商品的钱数即可#include #include #include #include using namespace std;struct Node{ int id; int amount; vector neighbor; Node(){} Node(int i, int a) : id(i),

2016-02-28 19:45:03 728

原创 1078. Hashing (25)

考查散列表的二次探测再散列#include #include #include using namespace std;bool isPrime(int n){ if(n <= 1) return false; for(int i = 2; i*i <= n; ++i){ if(n%i == 0) return false; } return true;}

2016-02-28 19:08:14 276

原创 1077. Kuchiguse (20)

#include #include using namespace std;int main(){ int n; (cin >> n).get(); string common; for(int i = 0; i < n; ++i){ string s; getline(cin, s); if(i == 0) common = s; else{ stri

2016-02-28 16:24:42 287

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除