写个博客,纪念一下今天的比赛吧。。。
Look-and-say sequence is a sequence of integers as the following:
D, D1, D111, D113, D11231, D112213111, ...
where D is in [0, 9] except 1. The (n+1)st number is a kind of description of the nth number. For example, the 2nd number means that there is one D in the 1st number, and hence it is D1; the 2nd number consists of one D (corresponding to D1) and one 1 (corresponding to 11), therefore the 3rd number is D111; or since the 4th number is D113, it consists of one D, two 1's, and one 3, so the next number must be D11231. This definition works for D = 1 as well. Now you are supposed to calculate the Nth number in a look-and-say sequence of a given digit D.
Input Specification:
Each input file contains one test case, which gives D (in [0, 9]) and a positive integer N (<=40), separated by a space.
Output Specification:
Print in a line the Nth number in a look-and-say sequence of D.
Sample Input:1 8Sample Output:
1123123111
这题理解题意就好了
意思是它给你一个数字D([0,9]),作为第一个序列,然后求第N个序列其中第n个序列是用来描述第n-1个序列的,如D113,下一个就是D11231,就是按顺序来D一个,1两个,3一个,组成D11231
#include
using namespace std;
const int MAXN = 100005;
const double eps = 1e-8;
char seq[MAXN], seq2[MAXN];
int main() {
//freopen("/1.txt", "r", stdin);
int D, n, j, cnt, t;
int k1, k2; //两个序列的长度
scanf("%d%d", &D, &n);
k1 = 0;
seq[k1++] = D;
seq[k1++] = 0;
for(int i = 1; i < n; ++i){
//转换n-1次,找出第n个序列
k2 = j = 0;
cnt = 1;
while(j < k1 - 1) {
t = seq[j++];
cnt = 1; //数字t出现的次数
while(j < k1 - 1 && t == seq[j])
++j, ++cnt;
seq2[k2++] = t;
seq2[k2++] = cnt;
}
seq2[k2++] = 0;
strcpy(seq, seq2);
k1 = k2;
}
for(int i = 0; i < k1 - 1; ++i)
printf("%c", seq[i] + '0');
return 0;
}
After each PAT, the PAT Center will announce the ranking of institutions based on their students' performances. Now you are asked to generate the ranklist.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (<=105), which is the number of testees. Then N lines follow, each gives the information of a testee in the following format:
ID Score School
where "ID" is a string of 6 characters with the first one representing the test level: "B" stands for the basic level, "A" the advanced level and "T" the top level; "Score" is an integer in [0, 100]; and "School" is the institution code which is a string of no more than 6 English letters (case insensitive). Note: it is guaranteed that "ID" is unique for each testee.
Output Specification:
For each case, first print in a line the total number of institutions. Then output the ranklist of institutions in nondecreasing order of their ranks in the following format:
Rank School TWS Ns
where "Rank" is the rank (start from 1) of the institution; "School" is the institution code (all in lower case); "TWS" is the total weighted score which is defined to be the integer part of "ScoreB/1.5 + ScoreA + ScoreT*1.5", where "ScoreX" is the total score of the testees belong to this institution on level X; and "Ns" is the total number of testees who belong to this institution.
The institutions are ranked according to their TWS. If there is a tie, the institutions are supposed to have the same rank, and they shall be printed in ascending order of Ns. If there is still a tie, they shall be printed in alphabetical order of their codes.
Sample Input:10 A57908 85 Au B57908 54 LanX A37487 60 au T28374 67 CMU T32486 24 hypu A66734 92 cmu B76378 71 AU A47780 45 lanx A72809 100 pku A03274 45 hypuSample Output:
5 1 cmu 192 2 1 au 192 3 3 pku 100 1 4 hypu 81 2 4 lanx 81 2
这题emmmm...最后两个测试用例超时了,最后才想到用Map存,以学校名为键值,第一次用Map,想不到会是在这样的场景现学的....
设计好合适的结构体存就好了
#include
using namespace std;
const int MAXN = 100005;
const double eps = 1e-8;
char seq[MAXN], seq2[MAXN];
struct School{
char name[10];
int cnt; //参与比赛的人数
double score; //总分数
int scoreInt;
School(char a[], int b, double c, int d) : cnt(b), score(c), scoreInt(d) {
strcpy(name, a);
}
bool operator < (School s) {
if(scoreInt == s.scoreInt) {
if(cnt == s.cnt)
return strcmp(name, s.name) < 0;
return cnt < s.cnt;
}
return scoreInt > s.scoreInt;
}
};
int main() {
//freopen("/1.txt", "r", stdin);
int n, k, t;
double score;
char ID[10], name[10];
vector vec;
map mm; //mm存学校名对应的下标
string str;
scanf("%d", &n);
while(n--) {
scanf("%s%lf%s", ID, &score, name);
if(ID[0] == 'T')
score *= 1.5;
else if(ID[0] == 'B')
score /= 1.5;
for(int i = 0; name[i]; ++i)
if(name[i] < 'a')
name[i] += 32;
str = name;
if(mm.find(str) == mm.end()) {
//新学校
mm[str] = vec.size();
//加上eps,防止出现数字1存成0.999999,int转换一下变成0的情况
vec.push_back(School(name, 1, score, (int)(score + eps)));
} else {
t = mm[str];
++vec[t].cnt;
vec[t].score += score;
vec[t].scoreInt = (int)(vec[t].score + eps);
}
}
sort(vec.begin(), vec.end());
printf("%d\n", vec.size());
k = 0;
while(k < vec.size()) {
int Rank = k + 1;
t = vec[k].scoreInt;
while(k < vec.size() && vec[k].scoreInt == t) {
printf("%d %s %d %d\n", Rank, vec[k].name, vec[k].scoreInt, vec[k].cnt);
++k;
}
}
return 0;
}
A clique is a subset of vertices of an undirected graph such that every two distinct vertices in the clique are adjacent. A maximal clique is a clique that cannot be extended by including one more adjacent vertex. (Quoted from https://en.wikipedia.org/wiki/Clique_(graph_theory))
Now it is your job to judge if a given subset of vertices can form a maximal clique.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers Nv (<= 200), the number of vertices in the graph, and Ne, the number of undirected edges. Then Ne lines follow, each gives a pair of vertices of an edge. The vertices are numbered from 1 to Nv.
After the graph, there is another positive integer M (<= 100). Then M lines of query follow, each first gives a positive number K (<= Nv), then followed by a sequence of K distinct vertices. All the numbers in a line are separated by a space.
Output Specification:
For each of the M queries, print in a line "Yes" if the given subset of vertices can form a maximal clique; or if it is a clique but not a maximal clique, print "Not Maximal"; or if it is not a clique at all, print "Not a Clique".
Sample Input:8 10 5 6 7 8 6 4 3 6 4 5 2 3 8 2 2 7 5 3 3 4 6 4 5 4 3 6 3 2 8 7 2 2 3 1 1 3 4 3 6 3 3 2 1Sample Output:
Yes Yes Yes Yes Not Maximal Not a Clique
这个题Nc最大才200,所以可以暴力过
对新输入的k个点,判断其是否可以构成cilque,
先判断这k个点是否相互邻接,不邻接输出 "Not a Clique"
若相互邻接,再寻找是否至少存在一个点(不在这k个点内),使得他与其他点都邻接,若存在输出"Not Maximal"
不存在输出"Yes"
#include
using namespace std;
const int MAXN = 205;
const double eps = 1e-8;
char seq[MAXN], seq2[MAXN];
bool G[MAXN][MAXN];
int Nv;
//寻找是否至少存在一个点使得其与k个连相邻接
bool found(int a[], int k, bool tag[]) {
for(int i = 1; i <= Nv; ++i)
if(!tag[i]) {
bool flag = true;
for(int j = 0; j < k; ++j) {
if(!G[i][a[j]]) {
flag = false;
break;
}
}
if(flag)
return true;
}
return false;
}
int main() {
//freopen("/1.txt", "r", stdin);
int Ne, m, u, v, k;
int a[MAXN];
bool tag[MAXN];
scanf("%d%d", &Nv, &Ne);
while(Ne--) {
scanf("%d%d", &u, &v);
G[u][v] = G[v][u] = 1;
}
scanf("%d", &m);
while(m--) {
scanf("%d", &k);
bool flag = true;
memset(tag, 0, sizeof(tag));
for(int i = 0; i < k; ++i) {
scanf("%d", &a[i]);
tag[a[i]] = true; //标记这k个点
for(int j = 0; j < i; ++j)
if(!G[a[i]][a[j]]) { //存在两个不连通的点,咔掉
flag = false;
break;
}
}
if(!flag) {
puts("Not a Clique");
continue;
}
//可以构成一个clique,再判断能否找到更大的clique
if(found(a, k, tag))
puts("Not Maximal");
else
puts("Yes");
}
return 0;
}
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants.
A binary search tree (BST) is recursively defined as a binary tree which has the following properties:
Given any two nodes in a BST, you are supposed to find their LCA.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers: M (<= 1000), the number of pairs of nodes to be tested; and N (<= 10000), the number of keys in the BST, respectively. In the second line, N distinct integers are given as the preorder traversal sequence of the BST. Then M lines follow, each contains a pair of integer keys U and V. All the keys are in the range of int.
Output Specification:
For each given pair of U and V, print in a line "LCA of U and V is A." if the LCA is found and A is the key. But if A is one of U and V, print "X is an ancestor of Y." where X is A and Y is the other node. If U or V is not found in the BST, print in a line "ERROR: U is not found." or "ERROR: V is not found." or "ERROR: U and V are not found.".
Sample Input:6 8 6 3 1 2 5 4 8 7 2 5 8 7 1 9 12 -3 0 8 99 99Sample Output:
LCA of 2 and 5 is 3. 8 is an ancestor of 7. ERROR: 9 is not found. ERROR: 12 and -3 are not found. ERROR: 0 is not found. ERROR: 99 and 99 are not found.
给出先序的二叉搜索树,查找两个点的公共祖先
建一个结构体,先写个先序遍历,把结构体节点都补充完整,-1表示没有孩子节点
对两个输入的数都进行从根节点到叶子节点的查找(找不到用-1表示,ui,vi),查找时第一个数时存下路径上的节点,查找第二个时比较路径上的节点与第一个路径上点是否相同,相同即为公共祖先,祖先向下查找更新,最新的公共祖先为最近的公共祖先
#include
using namespace std;
const int MAXN = 10005;
const double eps = 1e-8;
struct Node{
int l, r;
int val;
Node() : l(-1), r(-1) {};
}node[MAXN];
void preOrder(int l, int r) {
if(l <= r) {
//二叉搜索树,第一个点是根节点
int l2 = r + 1; //若有右子树,l2小于等于r
for(int i = l + 1; i <= r; ++i)
if(node[l].val < node[i].val) {
l2 = i;
break;
}
if((l2 <= r && l < l2 - 1) || (l2 > r) && (l < r))
node[l].l = l + 1;
if(l2 <= r)
node[l].r = l2;
if(l2 <= r)
preOrder(l + 1, l2 - 1);
else
preOrder(l + 1, r);
preOrder(l2, r);
}
}
int main() {
//freopen("/1.txt", "r", stdin);
int m, n, u, v, ui, vi, anc;
vector vec;
scanf("%d%d", &m, &n);
for(int i = 0; i < n; ++i)
scanf("%d", &node[i].val);
preOrder(0, n - 1);
// for(int i = 0; i < n; ++i)
// printf("%d %d %d\n", node[i].val, node[i].l, node[i].r);
while(m--) {
scanf("%d%d", &u, &v);
vec.clear();
ui = vi = 0;
while(true) {
vec.push_back(node[ui].val); //将寻找u的途中,经过的节点值都存储起来
if(u == node[ui].val)
break;
if(u < node[ui].val)
ui = node[ui].l;
else
ui = node[ui].r;
if(ui == -1)
break;
}
int k = 0;
while(true) {
//k可以理解成层数,若是u,v的共同祖先,它一定在同一层上;不断更新anc,能找到u,v的最近的祖先
if(k < vec.size() && node[vi].val == vec[k])
anc = vec[k];
++k;
if(v == node[vi].val)
break;
if(v < node[vi].val)
vi = node[vi].l;
else
vi = node[vi].r;
if(vi == -1)
break;
}
if(ui > -1 && vi > -1) {
if(anc == u)
printf("%d is an ancestor of %d.\n", anc, v);
else if(anc == v)
printf("%d is an ancestor of %d.\n", anc, u);
else
printf("LCA of %d and %d is %d.\n", u, v, anc);
}
else {
if(ui == -1 && vi == -1)
printf("ERROR: %d and %d are not found.\n", u, v);
else if(ui == -1)
printf("ERROR: %d is not found.\n", u);
else
printf("ERROR: %d is not found.\n", v);
}
}
return 0;
}