getchar

BZOJ 2733 永无乡

霸气de小男生 提交于 2020-01-01 04:58:14
splay启发式合并 启发式合并其实就是把集合数量小的合并到集合数量大的里去。 怎么合并呢,直接一个一个插入就行了。。 用并查集维护连通性,find(i)可以找到所在splay的编号 这题好像还可以合并线段树来写,下次再补上。。 #include <bits/stdc++.h> #define INF 0x3f3f3f3f #define full(a, b) memset(a, b, sizeof a) using namespace std; typedef long long ll; inline int lowbit(int x){ return x & (-x); } inline int read(){ int X = 0, w = 0; char ch = 0; while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); } while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar(); return w ? -X : X; } inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; } inline int lcm(int a, int b){ return a /

EOF reading C/C++

柔情痞子 提交于 2019-12-31 04:50:08
问题 I'm using NetBeans MinGW to compile simple c programs(I'm new at this). My problem is that I have this simple code #include <stdio.h> #include <stdlib.h> int main(int argc, char** argv) { int c,i=0; while((c=getchar())!=EOF){ i++; } printf("%d",i); return 0; } and when I try to end input like this: hello^Z [enter] it doesn't do it, I need to re-enter ^Z[enter] to end it. I'd appreciate that you tell me why this happens. Thanks in advance 回答1: C input is line-oriented by default. Ending a line

i want to getchar twice but i cant

别来无恙 提交于 2019-12-31 03:04:23
问题 int main() { int r, c; r = getchar(); c = getchar(); putchar(r); putchar(c); printf("\n"); return(0); } After it reads in r , the program outputs r and quits. I want it to ask for c and input it but how come it doesnt do that? 回答1: Are you entering the characters on the same line, or on 2 lines? getchar() will wait until you press enter, and then start parsing the characters. If you have entered 2 characters on 2 different lines, it will read the first character and then the \n character .

i want to getchar twice but i cant

随声附和 提交于 2019-12-31 03:04:11
问题 int main() { int r, c; r = getchar(); c = getchar(); putchar(r); putchar(c); printf("\n"); return(0); } After it reads in r , the program outputs r and quits. I want it to ask for c and input it but how come it doesnt do that? 回答1: Are you entering the characters on the same line, or on 2 lines? getchar() will wait until you press enter, and then start parsing the characters. If you have entered 2 characters on 2 different lines, it will read the first character and then the \n character .

初涉点分治

这一生的挚爱 提交于 2019-12-30 02:50:33
不能说是一个算法,应该算是一类思想 点分治 概念 点分治就是把树上问题中的节点拿来 分治 。 这所谓的“分治”是一个很抽象的概念,那么就先来介绍它的常见应用和其他性质。 大致框架 1 void getRoot(int x, int fa) //找重心 2 { 3 size[x] = 1, son[x] = 0; 4 for (int i=head[x]; i!=-1; i=nxt[i]) 5 { 6 int v = edges[i].y; 7 if (v==fa||vis[v]) continue; //在点分树内寻找重心 8 getRoot(v, x), size[x] += size[v]; 9 son[x] = std::max(son[x], size[v]); 10 } 11 son[x] = std::max(son[x], tot-size[x]); 12 if (son[x] < son[root]) root = x; //root即重心 13 } 14 void dfs(int x, int fa, int c) 15 { 16 record distance_c //将长度为c的路径记录下来 17 for (int i=head[x]; i!=-1; i=nxt[i]) 18 { 19 int v = edges[i].y; 20 if (v==fa||vis

Why is this statement printed twice in while loop?

亡梦爱人 提交于 2019-12-30 02:26:10
问题 I wrote this simple program for practise: #include <stdio.h> #include <stdlib.h> #include <string.h> #define CLASSES 3 #define STUDENTS 4 int grades[CLASSES][STUDENTS]; int main(void) { int i = 1; char t,k; while(i == 1) { printf("\n\n\nMENU:\nEnter the grades(E)\nReport Grades(R)\nQuit(Q)\nYour choice: "); k = toupper(getchar()); printf("Input entered... %c\n", k); switch(k) { case 'E' : printf("Entering the grades..\n"); break; case 'R' : printf("Reporting the grades...\n"); break; case 'Q'

ZeptoLab Code Rush 2015 A. King of Thieves 暴力

眉间皱痕 提交于 2019-12-30 00:46:16
A. King of Thieves Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/526/problem/A Description In this problem you will meet the simplified model of game King of Thieves. In a new ZeptoLab game called "King of Thieves" your aim is to reach a chest with gold by controlling your character, avoiding traps and obstacles on your way. An interesting feature of the game is that you can design your own levels that will be available to other players. Let's consider the following simple design of a level. A dungeon consists of n segments located at a same vertical level, each

fgetc, fgets, getc, getchar, gets, ungetc - 输入字符和字符串

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-28 13:05:31
总览 (SYNOPSIS) #include <stdio.h> int fgetc(FILE *stream); char *fgets(char *s, int size, FILE *stream); int getc(FILE *stream); int getchar(void); char *gets(char *s); int ungetc(int c, FILE *stream); 描述 (DESCRIPTION) fgetc() 从 stream 流 中 读取 下一个 字符, 然后 从 unsigned char 类型转换 到 int 型 返回, 如果 到达 文件末尾 或 出现 错误 则 返回 EOF . getc() 等于 fgetc() , 只是 它 可能 以 宏 的 形式 实现, 并 多次 访问 stream 流. getchar() 等于 getc( stdin ) . gets() 从 stdin 读取 一行 字符串, 保存在 s 指向的 缓冲区 中, 读到 换行符(newline) 或 EOF 时 操作 结束, 同时 把 它们 替换为 '\0' . 该函数 不检查 缓冲区溢出 (参见 后面的 BUGS 节). fgets() 从 stream 流 中 读取 多至 size - 1 个 字符, 保存在 s 指向的 缓冲区 中, 读到 换行符

【算法】点分治初探

爱⌒轻易说出口 提交于 2019-12-27 07:23:17
参考: http://blog.csdn.net/nixinyis/article/details/65445466 【简介】   点分治是一类用于处理树上路径点权统计问题的算法, 其利用重心的性质降低复杂度。 【什么是重心】   某个其所有的子树中最大的子树节点数最少的点被称为重心,删去重心后,生成的多棵树尽可能平衡。 【重心的性质】   ①重心其所有子树的大小都不超过$\frac{n}{2}$。   ②树中所有点到某个点的距离和中,到树的重心的距离和是最小的,如果有两个重心,那么到它们的距离和相同。   ③把两棵树通过两个点相连得到一棵新的树,新的树的重心必定在连接两棵树的重心的路径上。   ④一棵树添加或删除一个节点,树的重心最多会移动一条边的位置。   点分治的复杂度基于重心的第一个性质。 【点分治】   点分治是对于每一棵子树,都求出它的重心,并且以重心为根跑一遍这棵子树并统计经过重心的路径,因为我们知道重心所有子树的大小都只有原树的一半,也就是我们这么做最多只会递归$logn$层,若一层的复杂度$O(f(n))$,则总的时间复杂度为$O(f(n)logn)$。   接下来以bzoj1468: Tree为例题讲一下点分治。   对于这道题,显然用上方说的方法,对于每一个子树求出dep,排序后两端指针往中间靠拢统计即可。但是可能会统计重复,如下图

后缀数组小结

馋奶兔 提交于 2019-12-27 04:23:03
目录 原理介绍 倍增算法 基数排序 数组含义 代码解释 height 数组的功能 例题讲解 洛谷P3809【模板】后缀排序 BZOJ : 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 BZOJ : 4566: [Haoi2016]找相同字符 前言 :Orz ShichengXiao 冬令营的时候就早解决了 字符串算法还是不能随意放弃啊 要认真学了!! 这个算法常用于解决字符串上的 \(\mathrm{LCP}\) 问题 和 一些字符串匹配的问题 这个算法思维难度不是很大 但是代码难度还是有一些的 想学好这个算法 一定要牢牢的记住各个数组的含义 不然容易弄混 原理介绍 还是先简单介绍一下原理吧 : 后缀数组就是将一个字符串的后缀全部进行排序 然后把下标存入一些数组里 用那些数组来进行字符串的一些常用操作 为了后缀排序 我们常常使用 \(O(n \log n)\) 的倍增算法 (而不用 \(O(n)\) 的 \(\mathrm{DC3}\) 因为它常数和空间大,并且十分不好写) 倍增算法 那接下来介绍一下倍增算法qwq 考虑这样一个小问题 我们比较任意两后缀的字典序大小 有没有什么快速比较的方法? 当然有 就是预处理出他们的一个前缀和后缀的大小关系 然后我们就能用另外两个来比较了。 倍增的思路大概就是如此 我们从小到大 每次长度乘二