PAT甲级考前整理一:https://www.cnblogs.com/jlyg/p/7525244.html,主要讲了131题的易错题及坑点

PAT甲级考前整理二:https://www.cnblogs.com/jlyg/p/10364696.html,主要讲了考前注意以及一些常用算法。

1132题:用字符串接收会毕竟快,使用atoi函数转成数字,注意a*b会超出int32。

#include<iostream>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<iterator>
#include<algorithm>
#include<cstring>
using namespace std;

int main()
{
    #ifdef ONLINE_JUDGE
    #else
        freopen("test.txt","r",stdin);
    #endif
    int t;
    scanf("%d",&t);
    while(t--)
    {
        char str[20];
        scanf("%s",str);
        char str1[20],str2[20];
        int len = strlen(str);
        strncpy(str1,str,len/2);
        str1[len/2]='\0';
        strcpy(str2,str+len/2);
        int a = atoi(str1),b=atoi(str2);
        int c = atoi(str);
        //a*b超出int32
        if(a*b<=0||c%(a*b)) printf("No\n");
        else printf("Yes\n");
    }
    return 0;
}
View Code

1133题:因为k是正数,用3个vector存储就ok了,v0表示存储负数,v1表示存储小于等于k的,v2表示存储大于k的。

#include<iostream>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<iterator>
#include<algorithm>
#include<cstring>
using namespace std;
#define N (100010)
struct ST
{
    int addr;
    int val;
    int next;
};
ST sts[N];
int main()
{
    #ifdef ONLINE_JUDGE
    #else
        freopen("test.txt","r",stdin);
    #endif
    int startaddr,n,k;
    scanf("%d%d%d",&startaddr,&n,&k);
    for(int i=0;i<n;++i)
    {
        ST st;
        scanf("%d%d%d",&st.addr,&st.val,&st.next);
        sts[st.addr] = st;
    }
    vector<ST> vs;
    for(int addr=startaddr;addr!=-1;addr=sts[addr].next)
        vs.push_back(sts[addr]);
    vector<ST> vs0/*负数*/,vs1/*比k小或等于的正数*/,vs2/*比k大的正数*/;
    for(int i=0;i<vs.size();++i)
    {
        if(vs[i].val < 0)   vs0.push_back(vs[i]);
        else if(vs[i].val<=k)    vs1.push_back(vs[i]);
        else if(vs[i].val>k)   vs2.push_back(vs[i]);
    }

    {
        vs = vs0;
        for(int i=0;i<vs1.size();++i) vs.push_back(vs1[i]);
        for(int i=0;i<vs2.size();++i) vs.push_back(vs2[i]);

        for(int i=0;i<vs.size();++i)
        {
            if(i!=vs.size()-1)
                printf("%05d %d %05d\n",vs[i].addr,vs[i].val,vs[i+1].addr);
            else
                printf("%05d %d -1\n",vs[i].addr,vs[i].val);
        }
    }

    return 0;
}
View Code

1134题:题意有点难懂,顶点覆盖,即所有边的(边有两个顶点,任一一个顶点)某个顶点必须是在已给的顶点集合中。如果用遍历会超时,所以要用hash的想法。

#include<iostream>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<iterator>
#include<algorithm>
#include<cstring>
using namespace std;
#define N (10010)
struct Edge //记录边的两个端点
{
    int a;
    int b;
};
int main()
{
    #ifdef ONLINE_JUDGE
    #else
        freopen("test.txt","r",stdin);
    #endif
    int n,m;
    vector<Edge> ve;
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;++i)
    {
        Edge edge;
        scanf("%d%d",&edge.a,&edge.b);
        ve.push_back(edge);
    }
    int k;
    scanf("%d",&k);
    while(k--)
    {
        int nv;
        scanf("%d",&nv);
        bool flag = true;//是否是顶点覆盖
        bool V[N];
        memset(V,0,sizeof(V));        //下标表示定点,值表示是否有无改点
        for(int i=0;i<nv;++i)
        {
            int a;
            scanf("%d",&a);
            V[a] = true;
        }

        for(int i=0;i<ve.size();++i)
        {
            flag = V[ve[i].a]||V[ve[i].b];
            if(flag==false)
                break;
        }
        if(flag) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}
View Code

1135题:红黑树的题目,给出先序(插入顺序)构造红黑树,然后判断是否是红黑树。可以看我写的另一篇博客:

https://www.cnblogs.com/jlyg/p/7542409.html

1136题:简单题,大数加法加字符串反转

#include<iostream>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<iterator>
#include<algorithm>
#include<cstring>
using namespace std;
string Add(string str1,string str2)
{
    int len = str1.length();
    int add = 0;
    string res = "";
    for(int i=len-1;i>=0;--i)
    {
        int k = str1[i]-'0'+str2[i]-'0' + add;
        add = k/10;
        res += (k%10+'0');
    }
    if(add) res += (add+'0');
    reverse(res.begin(),res.end());
    return res;
}
int main()
{
    #ifdef ONLINE_JUDGE
    #else
        freopen("test.txt","r",stdin);
    #endif // ONLINE_JUDGE
    char temp[1010];
    scanf("%s",temp);
    string str = temp,str2=temp;
    for(int i=0;i<10;++i)
    {
         reverse(str2.begin(),str2.end());
        if(strcmp(str.c_str(),str2.c_str())==0)
        {
            printf("%s is a palindromic number.\n",str.c_str());
            return 0;
        }
        string str3 = Add(str,str2);
        printf("%s + %s = %s\n",str.c_str(),str2.c_str(),str3.c_str());
        str = str2 = str3;
    }
    printf("Not found in 10 iterations.\n");
    return 0;
}
View Code

1137题:简单题,Gm若是大于Gf,则G=(0.4*Gm+0.6*Gf+0.5),否则G = Gf; Gp<200和G<60的不记录。

#include<iostream>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<iterator>
#include<algorithm>
#include<cstring>
using namespace std;
struct ST
{
    string id;
    int Gp;
    int Gm;
    int Gf;
    int G;
    ST(){Gp=Gm=Gf=G=-1;}
    void UpdateG()
    {
        if(Gm > Gf) G = int(Gm*0.4f+Gf*0.6f+0.5f);
        else G = Gf;
    }
    bool IsUseful()
    {
        if(Gp<200) return false;
        if(G<60) return false;
        return true;
    }
};
int cmp(const ST& st1,const ST& st2)
{
    if(st1.G != st2.G) return st1.G>st2.G;
    return strcmp(st1.id.c_str(),st2.id.c_str())<0;
}
int main()
{
    #ifdef ONLINE_JUDGE
    #else
        freopen("test.txt","r",stdin);
    #endif // ONLINE_JUDGE

    int p,m,n;
    scanf("%d%d%d",&p,&m,&n);
    map<string,ST> mss;
    for(int i=0;i<p;++i)
    {
        char strid[30];
        int g;
        scanf("%s%d",strid,&g);
        mss[strid].Gp = g;
        mss[strid].id = strid;
    }
    for(int i=0;i<m;++i)
    {
        char strid[30];
        int g;
        scanf("%s%d",strid,&g);
        mss[strid].Gm = g;
        mss[strid].id = strid;
    }
    for(int i=0;i<n;++i)
    {
        char strid[30];
        int g;
        scanf("%s%d",strid,&g);
        mss[strid].Gf = g;
        mss[strid].id = strid;
    }
    vector<ST> vs;
    for(map<string,ST>::iterator it = mss.begin();it!=mss.end();it++)
    {
        it->second.UpdateG();
        if(it->second.IsUseful())
            vs.push_back(it->second);
    }
    sort(vs.begin(),vs.end(),cmp);
    for(int i=0;i<vs.size();++i)
    {
        printf("%s %d %d %d %d\n",vs[i].id.c_str(),vs[i].Gp,vs[i].Gm,vs[i].Gf,vs[i].G);
    }
    return 0;
}
View Code

1138题:简单题,先序和中序求后序第一个元素的值。

#include<iostream>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<iterator>
#include<algorithm>
#include<cstring>
using namespace std;
int FindFirst(int* pre,int* inorder,int n)
{
    if(n==1) return *pre;
    int val = *pre;
    int iIndex;
    for(iIndex=0;iIndex<n;++iIndex)
    {
        if(val == inorder[iIndex])
        {
            break;
        }
    }
    if(iIndex>0)
    {
        return FindFirst(pre+1,inorder,iIndex);
    }
    else
    {
        return FindFirst(pre+iIndex+1,inorder+iIndex+1,n-iIndex-1);
    }
}
int main()
{
    #ifdef ONLINE_JUDGE
    #else
        freopen("test.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int n;
    scanf("%d",&n);
    int pre[n],inorder[n];
    for(int i=0;i<n;++i)
        scanf("%d",&pre[i]);
    for(int i=0;i<n;++i)
        scanf("%d",&inorder[i]);
    printf("%d\n",FindFirst(pre,inorder,n));
    return 0;
}
View Code

 1139题:有点难度,注意两点:1)0000是男生,-0000是女生。2)使用递归dfs最后一个case会超时,老老实实写几层循环去遍历吧!

#include<iostream>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<iterator>
#include<algorithm>
#include<cstring>
using namespace std;
#define N (10010)
int cmp(const vector<int>& v1,const vector<int>& v2)
{
    if(v1[0] != v2[0]) return v1[0] < v2[0];
    return v1[1] < v2[1];
}
vector<int> vssame[N];   //相同
vector<int> vsdif[N];    //不同
bool bVis[N];
int thepeo1,thepeo2;
bool bSame;
vector<vector<int> > res;
void dfs()
{
    res.clear();
    memset(bVis,0,sizeof(bVis));
    bVis[thepeo1] = true;
    for(int i=0;i<vssame[thepeo1].size();++i)
    {
        int id1 = vssame[thepeo1][i];
        //printf("1:%04d\n",id1);
        if(!bVis[id1]&&id1!=thepeo2)
        {
            bVis[id1] = true;
            vector<int> *pvs;
            if(bSame) pvs = &(vssame[id1]);
            else pvs = &(vsdif[id1]);
            for(int j=0;j<pvs->size();++j)
            {
                int id2 = (*pvs)[j];
                //printf("  2:%04d\n",id2);
                if(!bVis[id2]&&id2!=thepeo2)
                {
                    bVis[id2] = true;
                    for(int k=0;k<vssame[id2].size();++k)
                    {
                        int id3 = vssame[id2][k];
                        //printf("     3:%04d\n",id3);
                        if(id3==thepeo2)
                        {
                            vector<int> vi;
                            vi.push_back(id1);
                            vi.push_back(id2);
                            res.push_back(vi);
                            break;
                        }
                    }
                    bVis[id2] = false;
                }

            }
            bVis[id1] = false;
        }
    }
}
int main()
{
    #ifdef ONLINE_JUDGE
    #else
        freopen("test.txt","r",stdin);
    #endif // ONLINE_JUDGE

    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;++i)
    {
        char str1[20],str2[20];
        scanf("%s%s",str1,str2);
        int id1 = abs(atoi(str1)),id2=abs(atoi(str2));
        if(str1[0]=='-'&&str2[0]=='-'
           ||str1[0]!='-'&&str2[0]!='-')
        {
            vssame[id1].push_back(id2);
            vssame[id2].push_back(id1);
        }
        else
        {
            vsdif[id1].push_back(id2);
            vsdif[id2].push_back(id1);
        }
    }
    int t;
    scanf("%d",&t);
    while(t--)
    {
        char str1[20],str2[20];
        scanf("%s%s",str1,str2);
        if(str1[0]=='-'&&str2[0]=='-'||str1[0]!='-'&&str2[0]!='-')
        {
            bSame = true;
        }
        else
        {
            bSame = false;
        }
        thepeo1 = abs(atoi(str1));
        thepeo2 = abs(atoi(str2));
        dfs();
        sort(res.begin(),res.end(),cmp);
        printf("%d\n",res.size());
        for(int i=0;i<res.size();++i)
        {
            vector<int>& vi = res[i];
            for(int j=0;j<vi.size();++j)
            {
                if(j) printf(" ");
                printf("%04d",vi[j]);
            }
            printf("\n");
        }
    }
    return 0;
}
View Code

 1140题:题意很难理解。。。一句话后面数字是前面数字的描述。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<algorithm>
using namespace std;
string getstr(string str)
{
    string res="";
    int len = str.length();
    char c= str[0],cnt=0;
    for(int i=0;i<len;++i)
    {
        if(c==str[i]) ++cnt;
        else if(c!=str[i])
        {
            res += c;
            char strcnt[1000];
            sprintf(strcnt,"%d",cnt);
            res +=strcnt;
            c = str[i];
            cnt = 1;
        }
        if(i==len-1)
        {
           res += c;
           char strcnt[1000];
           sprintf(strcnt,"%d",cnt);
           res +=strcnt;
        }
    }
    return res;
}
//题意:后面的数字是对前面数字的描述
int main()
{
    #ifdef ONLINE_JUDGE
    #else
        freopen("test.txt","r",stdin);
    #endif // ONLINE_JUDGE
    char a[100];
    int n;
    scanf("%s%d",&a,&n);
    string str = a;
    for(int i=1;i<n;++i)
        str=getstr(str);
    printf("%s\n",str.c_str());
    return 0;
}
View Code

 1142题:要求最大团:团是一组顶点,每个顶点两两可以组成边,最大团就是没有另外一个顶点与团里的点都能组成边。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
#define N (250)
int Map[N][N];
int nv,ne;
int main()
{
    #ifdef ONLINE_JUDGE
    #else
        freopen("test.txt","r",stdin);
    #endif // ONLINE_JUDGE
    scanf("%d%d",&nv,&ne);
    for(int i=0;i<ne;++i)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        Map[a][b] = Map[b][a] = 1;
    }
    int t;
    scanf("%d",&t);
    while(t--)
    {
       int k;
       scanf("%d",&k);
       int a[k];
       for(int i=0;i<k;++i)
          scanf("%d",&a[i]);
       bool bClique = true;
       for(int i=0;i<k;++i)
        for(int j=0;j<k;++j)
        {
           //要求每条边两两相交
           if(i!=j&&Map[a[i]][a[j]]==0)
           {
               bClique = false;
               break;
           }
        }
       if(bClique)
       {
           bool bMax = true;
           for(int i=1;i<=nv;++i)
           {
               int j;
               for(j=0;j<k;++j)
               {
                   if(Map[i][a[j]]==0)
                       break;
               }
               if(j==k)
               {
                   bMax = false;
                   break;
               }
           }
           if(bMax) printf("Yes\n");
           else printf("Not Maximal\n");
       }
       else printf("Not a Clique\n");
    }
    return 0;
}
View Code

 1143题:用map存储出现过的点,用于判断是否点存在。通过遍历先序,第一个出现满足a,b在val左右两边的就是他们的父节点。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
int main()
{
    #ifdef ONLINE_JUDGE
    #else
        freopen("test.txt","r",stdin);
    #endif // ONLINE_JUDGE
    map<int,bool> Map;
    int m,n;
    scanf("%d%d",&m,&n);
    int pre[n];
    for(int i=0;i<n;++i)
    {
        scanf("%d",&pre[i]);
        Map[pre[i]] = true;
    }
    while(m--)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        bool aexits = Map[a],bexits = Map[b];
        if(!aexits&&!bexits) printf("ERROR: %d and %d are not found.\n",a,b);
        else if(aexits&&bexits)
            for(int i=0;i<n;++i)
            {
                 int val = pre[i];
                 if(val>a&&val<b || val<a&&val>b)
                 {
                    printf("LCA of %d and %d is %d.\n",a,b,val);
                    break;
                 }
                else if(val==a|| val==b)
                {
                    printf("%d is an ancestor of %d.\n",val==a?a:b,val==a?b:a);
                    break;
                }
            }
        else printf("ERROR: %d is not found.\n",!aexits?a:b);
    }
    return 0;
}
View Code

 1144题:简单题,注意全部小于0是,应该输出1

#include<iostream>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<iterator>
#include<algorithm>
#include<cstring>
using namespace std;

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("test.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int n;
    scanf("%d",&n);
    int a[n];
    for(int i=0;i<n;++i)
        scanf("%d",&a[i]);
    sort(a,a+n);
    for(int i=1,j=0;j<n;++i)
        for(;j<n;++j)
        {
            if(a[j]==i) break;
            else if(a[j]>i)
            {
                printf("%d\n",i);
                return 0;
            }
        }
    printf("%d\n",max(a[n-1]+1,1));
    //printf("%d\n",a[n-1]);
    return 0;
}
View Code

1145题:hash的二次探测,题目比较坑 ,算查找的总次数,一般二次探测0到size-1次没找到就好了,但是它而要多加一,所以循环要写到0到size;

#include<iostream>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<iterator>
#include<algorithm>
#include<cstring>
using namespace std;
bool isPrime(int n)
{
    for(int i=2;i*i<=n;++i)
        if(n%i==0) return false;
    return true;
}
int getsize(int n)
{
    while(!isPrime(n)) ++n;
    return n;
}
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("test.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int msize,n,m;
    scanf("%d%d%d",&msize,&n,&m);
    msize = getsize(msize);
    int Hash[msize];
    memset(Hash,0,sizeof(Hash));
    for(int i=0;i<n;++i)
    {
        int a;
        scanf("%d",&a);
        int j;
        for(j=0;j<msize;++j)
            if(Hash[(a+j*j)%msize]==0)
            {
                Hash[(a+j*j)%msize] = a;
                break;
            }
        if(j>=msize) printf("%d cannot be inserted.\n",a);
    }
    int t = m,cnt=0;
    while(t--)
    {
        int a;
        scanf("%d",&a);
        for(int i=0;i<=msize;++i)
        {
            if(++cnt&&(Hash[(a+i*i)%msize]==a||(Hash[(a+i*i)%msize]==0)))
                break;
        }
    }
    printf("%.1lf\n",1.0*cnt/m);
    return 0;
}
View Code

1146题:拓扑排序,了解拓扑排序的原理就行了。学习拓扑排序必须知道什么是入度,有向图中有边:顶点a到顶点b,则b的入度是1。所以拓扑排序是每次输出入度为0的顶点,并且把该顶点相连的边的另一个顶点的入度减一。

#include<iostream>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<iterator>
#include<algorithm>
#include<cstring>
using namespace std;
#define N (1100)
int Map[N][N];
int indegree[N];
int n,m;
bool isTopuSort(int* a)
{
    int temp_indegree[N];
    memcpy(temp_indegree,indegree,sizeof(temp_indegree));
    for(int i=0;i<n;++i)
    {
        if(temp_indegree[a[i]]!=0) return false;
        for(int j=1;j<=n;++j) if(Map[a[i]][j]) --temp_indegree[j];
    }
    return true;
}
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("test.txt","r",stdin);
    #endif // ONLINE_JUDGE

    scanf("%d%d",&n,&m);
    for(int i=0;i<m;++i)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        ++indegree[b];
        Map[a][b] = 1;
    }
    int T;
    scanf("%d",&T);
    bool bFirst = true;
    for(int t=0;t<T;++t)
    {
        int a[n];
        for(int i=0;i<n;++i)
        {
            scanf("%d",&a[i]);
        }
        if(isTopuSort(a)==false)
        {
            if(bFirst) bFirst= false;
            else printf(" ");
            printf("%d",t);
        }
    }
    printf("\n");
    return 0;
}
View Code

 1147题:简单题,可以用不构造树的方式做。(当然构树也行,只是会麻烦一点~) heap tree。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
#define LEFT(i) (2*i+1)
#define RIGHT(i) (2*i+2)
bool isMaxHeap(int* a,int curI,int n)
{
    if(curI>=n) return true;
    int il = LEFT(curI),ir = RIGHT(curI);
    if(il<n&&a[curI]<a[il]) return false;
    if(ir<n&&a[curI]<a[ir]) return false;
    return isMaxHeap(a,il,n)&&isMaxHeap(a,ir,n);
}
bool isMinHeap(int* a,int curI,int n)
{
    if(curI>=n) return true;
    int il = LEFT(curI),ir = RIGHT(curI);
    if(il<n&&a[curI]>a[il]) return false;
    if(ir<n&&a[curI]>a[ir]) return false;
    return isMinHeap(a,il,n)&&isMinHeap(a,ir,n);
}
bool bFirst;
void post_travel(int* a,int curI,int n)
{
    if(curI>=n) return;
    post_travel(a,LEFT(curI),n);
    post_travel(a,RIGHT(curI),n);
    if(bFirst) bFirst = false;
    else printf(" ");
    printf("%d",a[curI]);
}
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("test.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int t,n;
    scanf("%d%d",&t,&n);
    while(t--)
    {
        int a[n];
        for(int i=0;i<n;++i)
            scanf("%d",&a[i]);
        if(isMaxHeap(a,0,n)) printf("Max Heap\n");
        else if(isMinHeap(a,0,n)) printf("Min Heap\n");
        else printf("Not Heap\n");
        bFirst = true;
        post_travel(a,0,n);
        printf("\n");
    }
    return 0;
}
View Code

 1148题:题目不好理解,有两个狼人,所有人中只有一狼一人说谎,输出两个狼人。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
int main(){
    #ifndef ONLINE_JUDGE
        freopen("test.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int n;
    scanf("%d",&n);
    int a[n+1];
    for(int i=1;i<=n;++i)
        scanf("%d",&a[i]);
    for(int i=1;i<=n;++i){
        for(int j=i+1;j<=n;++j){
            int bwolf[n+1];
            memset(bwolf,0,sizeof(bwolf));
            bwolf[i] = bwolf[j] = 1;    //i,j为狼
            bool bOK = (a[i]>0)==bwolf[abs(a[i])]&&(a[j]<0)==bwolf[abs(a[j])]||
            (a[i]<0)==bwolf[abs(a[i])]&&(a[j]>0)==bwolf[abs(a[j])];
            for(int k=1,lie=0;k<=n&&bOK;++k){
                if(i==k||j==k) continue;
                if((a[k]>0)==bwolf[abs(a[k])]){
                    ++lie;
                    if(lie>1) bOK = false;
                }
            }
            if(bOK){
                printf("%d %d\n",i,j);
                return 0;
            }
        }
    }
    printf("No Solution\n");
    return 0;
}
View Code

 1149题:简单题,两个物品放一起会爆炸,给出一串东西,判断这些东西有没有危险。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
set<int> misi[100010];
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("test.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;++i)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        misi[a].insert(b);
        misi[b].insert(a);
    }
    while(m--)
    {
        int k;
        scanf("%d",&k);
        vector<int> vi;
        bool bOK = true;
        for(int i=0;i<k;++i)
        {
            int a;
            scanf("%d",&a);
            if(bOK)
            {
                set<int>& sidag=misi[a];
                for(int j=0;j<vi.size();++j)
                {
                    if(sidag.find(vi[j])!=sidag.end())
                    {
                        bOK = false;
                        break;
                    }
                }
                vi.push_back(a);
            }
        }
        printf("%s\n",bOK?"Yes":"No");
    }
    return 0;
}
View Code

 1150题:简单题

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
using namespace std;

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("test.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int n,m;
    scanf("%d%d",&n,&m);
    int Map[n+1][n+1];
    memset(Map,0,sizeof(Map));
    for(int i=0;i<m;++i)
    {
        int a,b,l;
        scanf("%d%d%d",&a,&b,&l);
        Map[a][b] = Map[b][a] = l;
    }
    int T;
    int minLen = 1<<30;
    int minPath = -1;
    scanf("%d",&T);
    for(int t=1;t<=T;++t)
    {
        printf("Path %d: ",t);
        int cn;
        scanf("%d",&cn);
        int istart,iend;
        bool bVis[n+1];
        memset(bVis,0,sizeof(bVis));
        bool bSimple = true;
        int cnt = 0,pre,len=0;
        int bNA = false;
        for(int i=0;i<cn;++i)
        {
            int a;
            scanf("%d",&a);
            if(i&&Map[pre][a]==0)   bNA = true;
            else if(i) len += Map[pre][a];
            if(i==0) istart = a;
            if(i==cn-1) iend = a;
            pre = a;
            if(bVis[a]==0)
            {
                bVis[a] = 1;
                ++cnt;
            }
            else if(i!=cn-1)   bSimple = false;
        }
        if(bNA) printf("NA ");
        else printf("%d ",len);
        if(cnt!=n||bNA||istart!=iend) printf("(Not a TS cycle)\n");
        else
        {
            if(minLen > len)
            {
                minLen = len;
                minPath = t;
            }
            printf("(%s)\n",bSimple?"TS simple cycle":"TS cycle");
        }
    }
    printf("Shortest Dist(%d) = %d\n",minPath,minLen);
    return 0;
}
View Code

 1151题:用两个map,一个是把val转换成编号(从1开始,0表示没有),一个是把编号转换成val。中序输入的时候通过把val转换成编号,中序就是从小到大排序的,所以该树就变成搜索二叉树了。然后通过pre数组记录先序的编号,通过先前记录的map吧val转换成编号。所以该数时关于编号的搜索二叉树。然后接下来的做法就跟1143题一模一样了。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
using namespace std;

int main()
{
    #ifdef ONLINE_JUDGE
    #else
        freopen("test.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int m,n;
    scanf("%d%d",&m,&n);
    map<int,int> valtoid;
    map<int,int> idtoval;
    int pre[n];
    for(int i=0;i<n;++i)
    {
       int a;
       scanf("%d",&a);
       valtoid[a] = i+1;
       idtoval[i+1] = a;
    }
    for(int i=0;i<n;++i)
    {
       int a;
       scanf("%d",&a);
       pre[i] = valtoid[a];
    }
    while(m--)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        int aexits=valtoid[a],bexits=valtoid[b];
        if(!aexits&&!bexits) printf("ERROR: %d and %d are not found.\n",a,b);
        else if(!aexits||!bexits) printf("ERROR: %d is not found.\n",!aexits?a:b);
        else
        {
            for(int i=0;i<n;++i)
            {
                int ia = valtoid[a],ib = valtoid[b];
                if(pre[i]<ia&&pre[i]>ib||pre[i]<ib&&pre[i]>ia)
                {
                    printf("LCA of %d and %d is %d.\n",a,b,idtoval[pre[i]]);
                    break;
                }
                else if(pre[i]==ia||pre[i]==ib)
                {
                    printf("%d is an ancestor of %d.\n",pre[i]==ia?a:b,pre[i]==ia?b:a);
                    break;
                }
            }
        }
    }
    return 0;
}
View Code

 1152题:简单题,题目的tip也很详细。

#include<iostream>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<iterator>
#include<algorithm>
#include<cstring>
using namespace std;
#define L (1010)
bool isPrime(long long int a)
{
    if(a==0) return false;
    for(long long int i=2;i*i<=a&&i*i>0;++i) if(a%i==0) return false;
    return true;
}
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("test.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int l,k;
    scanf("%d%d",&l,&k);
    char str[L];
    scanf("%s",&str);
    string res="";
    for(int i=0;i<l;++i)
    {
        res += str[i];
        if(i+1>=k)
        {
            if(res.length()>k) res = res.substr(1,k);
            long long int a = atoll(res.c_str());
            if(isPrime(a))
            {
                printf("%s\n",res.c_str());
                return 0;
            }
        }
    }
    printf("404\n");
    return 0;
}
View Code

1153题:简单题,不要每次搜索1类型的时候都去排序,不然会超时。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
struct PAT
{
    string str;
    int sorce;
    string type;  //考试类型
    string sitnum;
    string date;
    string testeenum;
    void init(string str,int sorce)
    {
        this->str = str;
        this->sorce =sorce;
        int i = 0;
        sitnum = date = testeenum = "";
        type = str[i++];
        for(;i<4;++i) sitnum += str[i];
        for(;i<10;++i) date += str[i];
        for(;i<13;++i) testeenum+=str[i];
    }
};
int sitnum[1000];
PAT pats[10010];
int sitcmp(const int& a,const int& b)
{
    if(sitnum[a]!=sitnum[b]) return sitnum[a]>sitnum[b];
    return a<b;
}
int cmp(const int& a,const int& b)
{
    PAT p1 = pats[a],p2=pats[b];
    if(p1.sorce!=p2.sorce) return p1.sorce>p2.sorce;
    return strcmp(p1.str.c_str(),p2.str.c_str())<0;
}
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("test.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int n,m;
    scanf("%d%d",&n,&m);
    map<string,vector<int> > searchmap[3];
    for(int i=0;i<n;++i)
    {
        char str[30];
        int sorce;
        scanf("%s%d",str,&sorce);
        pats[i].init(str,sorce);
        searchmap[0][pats[i].type].push_back(i);
        searchmap[1][pats[i].sitnum].push_back(i);
        searchmap[2][pats[i].date].push_back(i);

    }
    set<string> bsort;      //关于1对应key的序列是否排序好了
    for(int i=0;i<m;++i)
    {
        int opt;
        char str[30];
        scanf("%d%s",&opt,str);
        printf("Case %d: %d %s\n",i+1,opt,str);
        vector<int> &vi = searchmap[opt-1][str];
        memset(sitnum,0,sizeof(sitnum));
        if(vi.size())
        {
            if(opt==1&&bsort.find(str)==bsort.end()) sort(vi.begin(),vi.end(),cmp),bsort.insert(str);
            int tsorce = 0;
            for(int j=0;j<vi.size();++j)
            {
                int index = vi[j];
                if(opt==1)  printf("%s %d\n",pats[index].str.c_str(),pats[index].sorce);
                else if(opt==2) tsorce += pats[index].sorce;
                else if(opt==3) ++sitnum[atoi(pats[index].sitnum.c_str())];
            }
            if(opt ==2) printf("%d %d\n",vi.size(),tsorce);
            else if(opt==3)
            {
                vector<int> sitvc;
                for(int j=0;j<1000;++j)
                    if(sitnum[j]) sitvc.push_back(j);
                sort(sitvc.begin(),sitvc.end(),sitcmp);
                for(int j=0;j<sitvc.size();++j)
                    printf("%03d %d\n",sitvc[j],sitnum[sitvc[j]]);
            }
        }
        else printf("NA\n");
    }
    return 0;
}
View Code

1154题:简单题

#include<iostream>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<iterator>
#include<algorithm>
#include<cstring>
using namespace std;

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("test.txt","r",stdin);
    #endif
    int n,m;
    scanf("%d%d",&n,&m);
    vector<int> Map[n];
    for(int i=0;i<m;++i)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        Map[a].push_back(b);
    }
    int t;
    scanf("%d",&t);
    while(t--)
    {
        set<int> si;
        int color[n];
        for(int i=0;i<n;++i)
        {
            scanf("%d",&color[i]);
            si.insert(color[i]);
        }
        bool bOK = true;
        for(int i=0;i<n&&bOK;++i)
            for(int j=0;j<Map[i].size();++j)
                if(color[i]==color[Map[i][j]])
                {
                    bOK = false;
                    break;
                }
        if(bOK) printf("%d-coloring\n",si.size());
        else printf("No\n");
    }
    return 0;
}
View Code

1155题:简单题,跟1147一样是heap tree。估计题目有点想出堆排序,但是又怕没人会堆排序所以搞成这样吧。。

#include<iostream>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<iterator>
#include<algorithm>
#include<cstring>
using namespace std;
#define LEFT(i) (2*i+1)
#define RIGHT(i) (2*i+2)
struct Node
{
    int val;
    Node* left;
    Node* right;
    Node(){left=right=NULL;}
};
Node* Build(int *a,int i,int n)
{
    if(i >= n) return NULL;
    Node* node = new Node();
    node->val = a[i];
    node->left = Build(a,LEFT(i),n);
    node->right = Build(a,RIGHT(i),n);
    return node;
}
void travel(Node* node,vector<Node*> vn)
{
    vn.push_back(node);
    if(!node->left&&!node->right)
    {
        for(int i=0;i<vn.size();++i)
        {
            if(i) printf(" ");
            printf("%d",vn[i]->val);
        }
        printf("\n");
    }
    if(node->right) travel(node->right,vn);
    if(node->left) travel(node->left,vn);
}
bool bMaxHeap(int* a,int i,int n)
{
    if(i>=n) return true;
    if(LEFT(i)<n&&a[i]<a[LEFT(i)]) return false;
    if(RIGHT(i)<n&&a[i]<a[RIGHT(i)]) return false;
    return bMaxHeap(a,LEFT(i),n)&&bMaxHeap(a,RIGHT(i),n);
}
bool bMinHeap(int* a,int i,int n)
{
    if(i>=n) return true;
    if(LEFT(i)<n&&a[i]>a[LEFT(i)]) return false;
    if(RIGHT(i)<n&&a[i]>a[RIGHT(i)]) return false;
    return bMinHeap(a,LEFT(i),n)&&bMinHeap(a,RIGHT(i),n);
}
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("test.txt","r",stdin);
    #endif
    int n;
    scanf("%d",&n);
    int a[n];
    for(int i=0;i<n;++i)
        scanf("%d",&a[i]);
    Node* root = Build(a,0,n);
    vector<Node*> vn;
    travel(root,vn);
    if(bMaxHeap(a,0,n)) printf("Max Heap\n");
    else if(bMinHeap(a,0,n)) printf("Min Heap\n");
    else printf("Not Heap\n");
    return 0;
}
View Code