SDUT 1

 A 2152 Phone Number:水    

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;

int cmp(string a,string b) {
    return a.compare(b)<0;
}

int main() {
    int n,i;
    string str[1001],temp;

    while (scanf("%d", &n)&& n) {
        for (i=0;i<n;i++)
            cin>>str[i];
        sort(str,str+n,cmp);
        for (i=0;i<n-1;i++){
            temp=str[i+1].substr(0,str[i].length());
            if (temp.compare(str[i])==0) {
                cout<<"NO"<<endl;
                break;
            }
        }
        if (i==n-1) cout<<"YES"<<endl;
    }
}
A 2151 Phone Number

 

C 2158 Hello World:水

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

int main(){

    int n,a[1001],b[1001],x,y,cnt=0,m[300][300],alen[300],t;
    while(scanf("%d",&n)&&n){
        cnt++;
        //printf("Case %d:\n",cnt);
        memset(m,0, sizeof(m));
        memset(alen,0, sizeof(alen));

        for (int i=0;i<n;i++){
            scanf("%d%d",&a[i],&b[i]);
            m[a[i]][alen[a[i]]]=b[i];
            alen[a[i]]++;
        }
        int j=1;
        while(j<300){
            sort(m[j],m[j]+alen[j]);
            j++;
        }

        printf("Case %d:\n",cnt);
        for (int i=0;i<n;i++) {
            x = a[i] + 1;
            y = b[i] + 1;

            while (x<300) {
                while (alen[x] == 0&&x<300) x++;
                if (x==300) break;
                for (t=0;t<alen[x];t++)
                    if (m[x][t]>=y) break;
                if (t<alen[x]) {
                    printf("%d %d\n",x,m[x][t]);
                    break;
                }else x++;
            }
            if (x>=300) printf("-1 -1\n");
        }
        printf("\n");
    }
    return 0;
}
C 2158 Hello World

 

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <set>
using namespace std;

int main(){

    int cnt=0,n;
    int a[1001],b[1001];
    set< pair<int,int> > m;
    set< pair<int,int> >::iterator iter;
    pair<int,int> p;

    while(scanf("%d",&n)&&n){
        cnt++;
        m.clear();
        printf("Case %d:\n",cnt);
        for (int i=0;i<n;i++) {
            scanf("%d%d", &a[i], &b[i]);
            m.insert(make_pair(a[i],b[i]));
        }

        for (int i=0;i<n;i++){
            p=make_pair(a[i]+1,b[i]);
            iter=m.upper_bound(p);
            while(iter!=m.end()){
                if (iter->second>b[i]) {
                    printf("%d %d\n",iter->first,iter->second);
                    break;
                }else  iter++;
            }
            if (iter==m.end()&&iter->second<=b[i]) printf("-1 -1\n");
            else if (iter==m.end()) printf("%d %d\n",iter->first,iter->second);
        }

        printf("\n");
    }
    return 0;
}
C 2158 Hello World(set+pair)

 

D 2157 Greatest Number

 

I 2152 Balloons:暴搜

#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;

struct Node{
    int first,second;
};
int xdir[] = {1,0,0,-1,-1,-1,1,1};
int ydir[] = {0,1,-1,0,-1,1,-1,1};

int main(){

    int n,map[102][102],flag[102][102],cnt=0,ans;
    char c;
    queue< Node > q;
    Node temp,t;

    while(scanf("%d",&n)&&n){
        cnt++;
        memset(map,0,sizeof(map));
        for (int i=1;i<=n;i++){
            scanf("\n");
            for (int j=1;j<=n;j++){
                scanf("%c",&c);
                if (c=='0') map[i][j]=0;
                else map[i][j]=1;
            }

        }
        printf("Case %d: ",cnt);

        memset(flag,0,sizeof(flag));
        ans=0;
        for (int i=1;i<=n;i++){
            for (int j=1;j<=n;j++){
                if (map[i][j]&&(!flag[i][j])) {
                    temp.first=i;temp.second=j;
                    q.push(temp);
                    flag[i][j]=1;
                    while(!q.empty()){
                        temp=q.front();
                        q.pop();

                        for(int z=0; z<4; z++) {
                            t.first = temp.first + xdir[z];
                            t.second = temp.second + ydir[z];
                            if(flag[t.first][t.second] == 0 && map[t.first][t.second]) {
                                flag[t.first][t.second] = 1;
                                q.push(t);
                            }
                        }
                    }
                    ans++;
                }//if
            }
        }
        printf("%d ",ans);

        memset(flag,0,sizeof(flag));
        ans=0;
        for (int i=1;i<=n;i++){
            for (int j=1;j<=n;j++){
                if (map[i][j]&&(!flag[i][j])) {
                    temp.first=i;temp.second=j;
                    q.push(temp);
                    flag[i][j]=1;
                    while(!q.empty()){
                        temp=q.front();
                        q.pop();
                        for(int z=0; z<8; z++) {
                            t.first = temp.first + xdir[z];
                            t.second = temp.second + ydir[z];
                            if(flag[t.first][t.second] == 0 && map[t.first][t.second]) {
                                flag[t.first][t.second] = 1;
                                q.push(t);
                            }
                        }
                    }
                    ans++;
                }//if
            }
        }
        printf("%d\n\n",ans);

    }
    return 0;
}
I 2152 Balloons

 

G 2154 Shopping:水

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int main() {
    int n;
    int a[100005];
    while (scanf("%d", &n)&& n) {
        memset(a,0,sizeof(a));
        for (int i = 0; i < n; i++)
            scanf("%d",&a[i]);
        sort(a,a+n);
        printf("%d\n",2*(a[n-1]-a[0]));
    }
}
G 2154 Shopping

 

C 可以用二位数组+复杂分支循环结构,或者结构体数组+自定义比较函数的sort,也可以用set+pair+自定义比较函数。题目相当于一个多关键字排序,但又是一个偏序关系(当a的行比b的行大,b的列比a的行大则无法比较),所以不能直接在自定义函数中描述出来,但还是倾向将题目中的关系线性存储。

 

D 暴搜肯定不行,一开始没看到4个数的限制,在考虑背包、贪心。否定背包是觉得开不了这么大的数组(资源分配,按背包大小和物品种类数动归),考虑过贪心每次取最大的装到满然后再取第二大的装到满即可(找硬币)。

I 广搜,注意一定要在刚入队前/后就标记,而不是等到出队的时候标记!

posted @ 2018-04-10 09:53  Travelller  阅读(198)  评论(0编辑  收藏  举报