PTA 1140 1141 1142 1143

1140 Look-and-say Sequence

思路:模拟

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
int d,n;
const int maxn = 10010;
int cnt[11];
vector<int> a;
vector<int> b;

int main(){
    scanf("%d%d",&d,&n);
    a.push_back(d);
    if(n == 1) {
        printf("%lld",d);
        return 0;
    }
    for(int i=2;i<=n;i++){
        int j = 0;
        b.clear();
        int len = a.size();
        while(j < len){
            int curv = a[j];
            int cnt = 0;
            while(j<len && a[j] == curv){
                cnt++;
                j++;
            }
            b.push_back(curv);
            b.push_back(cnt);
        }
        a.clear();
        for(int j=0;j<b.size();j++) a.push_back(b[j]);
    }
    for(int i=0;i<a.size();i++){
        printf("%d",a[i]);
    }
    return 0;
}

1141 PAT Ranking of Institutions

思路:结构体排序,map统计查询

#include<bits/stdc++.h>
#include<algorithm>
#include<string>
using namespace std;

int n;
const int maxn = 1e5+10;
struct node{
    string id;
    double score;
    string cap;
};

struct node stu[maxn];
map<string,int> mp;
map<string,double> scores;

struct capNode{
    string cap;
    int score;
    int nums;
    capNode(string ss,double cc,int num){
        cap = ss;
        score = cc;
        nums = num;
    }
};

vector<capNode> vec;

bool cmp(capNode x,capNode y){
    if(x.score == y.score) {
        if(x.nums == y.nums) return x.cap < y.cap;
        return x.nums < y.nums;
    }
    return x.score > y.score;
}

int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        string curCap;
        cin>>stu[i].id>>stu[i].score>>curCap;
        transform(curCap.begin(),curCap.end(),curCap.begin(),::tolower);
        stu[i].cap = curCap;
        mp[stu[i].cap]++;
        if(stu[i].id[0] == 'T'){
            scores[stu[i].cap] += stu[i].score*1.5;
        }else if(stu[i].id[0] == 'A'){
            scores[stu[i].cap] += stu[i].score;
        }else{
            scores[stu[i].cap] += stu[i].score*1.0/1.5;
        }
    }
    map<string,int>::iterator it = mp.begin();
    while(it!=mp.end()){
        string name = it->first;
        int num = it->second;
        int sc = (int)scores[name];
        vec.push_back(capNode(name,sc,num));
        it++;
    }
    sort(vec.begin(),vec.end(),cmp);
    int len = vec.size();
    printf("%d\n",len);
    if(len > 0){
        int lastScore = vec[0].score;
        int rank = 1;
        for(int i=0;i<len;i++){
            if(vec[i].score == lastScore){
                cout<<rank<<" "<<vec[i].cap<<" "<<vec[i].score<<" "<<vec[i].nums<<endl;
            }else{
                rank = i+1;
                lastScore = vec[i].score;
                cout<<rank<<" "<<vec[i].cap<<" "<<vec[i].score<<" "<<vec[i].nums<<endl;
            }
        }
    }
    return 0;
} 

1142 Maximal Clique

思路:图论,判断是否同一“集合”,按题目要求判断是否有边

#include<bits/stdc++.h>
using namespace std;

const int maxn = 210;
int n,ne;
int m;
int g[maxn][maxn];
int a[maxn];
int vis[maxn];
int k;

bool isClique(){
    for(int i=1;i<=k;i++){
        for(int j=1;j<=k;j++){
            if(i!=j && g[a[i]][a[j]] == 0) return false;
        }
    }
    return true;
}

bool isMaxClique(){
    for(int i=1;i<=n;i++) vis[i] = 0;
    for(int i=1;i<=k;i++) vis[a[i]] = 1;
    for(int i=1;i<=n;i++){
        if(!vis[i]){
            for(int p=1;p<=k;p++){
                for(int q=1;q<=k;q++){
                    if(p != q || (k==1)){ //这里特判k==1的情况  如样例图中查询1 8:8号结点应该输出Not Maximal
                        if(g[i][a[p]]==1 && g[i][a[q]] == 1){
                            return false;
                        }
                    }
                }
            }
        }
    }
    return true;
}

int main(){
    scanf("%d%d",&n,&ne);
    for(int i=1;i<=ne;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        g[u][v] = g[v][u] = 1;
    }
    scanf("%d",&m);
    for(int i=1;i<=m;i++){
        scanf("%d",&k);
        for(int j=1;j<=k;j++) scanf("%d",&a[j]);
        bool flag1 = isClique();
        bool flag2 = isMaxClique();
        if(flag1 == false){
            puts("Not a Clique");
        }else{
            if(flag2 == false){
                puts("Not Maximal");
            }else{
                puts("Yes");
            }
        }
    }
    return 0;
} 

1143 Lowest Common Ancestor 29/30

思路:给先序序列建树,找最近公共祖先
一个点段错误,有时间再补。。

#include<bits/stdc++.h>
using namespace std;

const int maxn = 10010;
int m,n;
struct node{
    int v;
    node *l;
    node *r;
};
int a[maxn];
set<int> se;
int deep[maxn];
int fa[maxn];

void getDeep(node *root,int depth,int father){
    if(root == NULL) return;
    deep[root->v] = depth;
    fa[root->v] = father;
    if(root->l != NULL) getDeep(root->l,depth+1,root->v);
    if(root->r != NULL) getDeep(root->r,depth+1,root->v);
}

void build(int pos,node *root){
    if(root == NULL) return;
    node* fa = root;
    int v = a[pos];
    if(v < fa->v){
        if(fa->l == NULL){
            node *cur = new node();
            cur->v = v;
            cur->l = NULL;
            cur->r = NULL;
            fa->l = cur;
            return;
        }
        build(pos,fa->l);
    }else{
        if(fa->r == NULL){
            node *cur = new node();
            cur->v = v;
            cur->l = NULL;
            cur->r = NULL;
            fa->r = cur;
            return;
        }
        build(pos,fa->r);
    }
    return;
}

int main(){
    scanf("%d%d",&m,&n);
    for(int i=1;i<=n;i++) {
        scanf("%d",&a[i]);
        se.insert(a[i]);
    }
    node *Root = new node();
    Root->l = NULL;
    Root->r = NULL;
    if(n>=1) Root->v = a[1];
    else Root->v = 0;
    if(n>=2) for(int i=2;i<=n;i++) build(i,Root);
    getDeep(Root,1,0);
    for(int i=1;i<=m;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        bool visu = true;
        bool visv = true;
        if(se.find(u) == se.end()) visu = false;
        if(se.find(v) == se.end()) visv = false;
        if(visu == false || visv == false){
            if(visu==false && visv==false) printf("ERROR: %d and %d are not found.\n",u,v);
            else if(visu == false) printf("ERROR: %d is not found.\n",u);
            else printf("ERROR: %d is not found.\n",v);
        }else{
            if(deep[u] < deep[v]){
                int p = u;
                int q = v;
                while(deep[q] > deep[p]) q = fa[q];
                if(q == p) printf("%d is an ancestor of %d.\n",u,v);
                else{
                    while(p != q){
                        p = fa[p];
                        q = fa[q];
                    }
                    printf("LCA of %d and %d is %d.\n",u,v,p);
                }
            }else{
                int p = u;
                int q = v;
                while(deep[p] > deep[q]) p = fa[p];
                if(p == q) printf("%d is an ancestor of %d.\n",v,u);
                else{
                    while(p != q){
                        p = fa[p];
                        q = fa[q];
                    }
                    printf("LCA of %d and %d is %d.\n",u,v,p);
                }
            }
        }
    }
    return 0;
} 

原文地址:https://www.cnblogs.com/fisherss/p/11651317.html

时间: 2024-08-30 17:59:33

PTA 1140 1141 1142 1143的相关文章

DirectShowNet 使用摄像头录像+录音

http://www.cnblogs.com/endv/p/6052511.html 1 // ------------------------------------------------------------------ 2 // CaptureTest.cs 3 // Sample application to show the DirectX.Capture class library. 4 // 5 // History: 6 // 2003-Jan-25 BL - created

微软原版SQLHelper类

C# Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

[自制简单操作系统] 3、内存管理和窗口叠加

1.本次主要进展 >_<" 这次主要学习了系统内存管理和窗口叠加~由于上两篇都做了详细的框架说明和介绍,这里直接上代码! 2.文件及函数构成 >_<" 这里和第二篇相比,把鼠标和键盘的相关函数独立出来放进各自相应的文件中,并主要在内存管理和窗口叠加进行探索,同时还有部分代码整理~ 1 /* In this file, not only have the defination of the function, but also 2 hava the descrip

gridview自定义分页 aspNetPager用法

这几天做gridview分页,自带的分页不好用.从网上搜刚开始搜了一个aspNetPager.dll,替换原来的一个dll,自己设置了一些,比如这个控件不显示中文,后来我改成中文的"上一页"."下一页"."首页""尾页",把ShowPageIndexBox设置为默认true,这样当前页也显示了,还能跳转. 还设置了 显示自定义区域,默认是不显示了,我改成left,然后再设置下自定义的html,结果就可以显示了,我显示的是 一共

cJSON_hacking

1 /****************************************************************************** 2 * cJSON_hacking 3 * 4 * 1.这是cJSON中cJSON.c(主程序)的源码,源码不到1000行(除注释). 5 * 2.本文仅仅注释了源码中JSON解析部分,对链表的操作没有进行任何注释,通过 6 * 分析阅读该源码,可以一窥双向链表,字符串处理,里面几个地方使用到了递归, 7 * 而且用得很精妙,但在理解

[自制简单操作系统] 2、鼠标及键盘中断处理事件[PIC\GDT\IDT\FIFO]

1.大致介绍: >_<" 大致执行顺序是:ipl10.nas->asmhead.nas->bootpack.c PS: 这里bootpack.c要调用graphic.c.dsctbl.c.fifo.c.int.c实现功能,其中有些函数还必须汇编来写,所以单独写一个汇编文件naskfunc.nas,为了方便看全部函数和结构体,所以写一个bootpack.h来写一些结构体和函数声明~ >_<" 下面是编译图解:最终生成的haribote.img可放在软盘

Linux系统开发 2 文件IO open() close() read() write() perror() lseek() fcntl() ioctl()

[本文谢绝转载,原文来自http://990487026.blog.51cto.com] 大纲 Linux系统开发 man 文档的使用 文件IO open() 创建文件,指定权限位 open() 接收参数 创建文件 open() 传两个参数 第三个参数从内存取垃圾值 write()函数 向文件写数据 write()函数的覆盖操作 open()函数文件的追加 open() 创建文件,如果文件已经存在,就报错 测试一个程序最多能创建1021个文件,3个STDIN STDOUT STDERR已经存在了

[C语言练习]学生学籍管理系统

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

10000以内unicode对照表

0 . 1 . 2 . 3 . 4 . 5 . 6 . 7 . 8   9   10   11   12   13 . 14 . 15 . 16 . 17 . 18 . 19 . 20 . 21 . 22 . 23 . 24 . 25 . 26 . 27 . 28 . 29 . 30 . 31   32 ! 33 " 34 # 35 $ 36 % 37 & 38 ' 39 ( 40 ) 41 * 42 + 43 , 44 - 45 . 46 / 47 0 48 1 49 2 50 3 5