codeforces 412 impelment、greedy

来源:互联网 发布:女生性幻想对象 知乎 编辑:程序博客网 时间:2024/05/18 05:03

A. Poster

http://codeforces.com/problemset/problem/412/A

要点:重复路径尽可能短(贪心)。

#include <iostream>#include <cstdio>using namespace std;char s[105];int n,k;void right(int &pos){    while(pos<n){        if(s[pos]!='*'){            printf("PRINT %c\n",s[pos]);            s[pos]='*';        }        //cout<<pos<<endl;        if(pos<n-1)printf("RIGHT\n");        pos++;    }    pos--;}void left(int &pos){    while(pos>=0){        if(s[pos]!='*'){            printf("PRINT %c\n",s[pos]);            s[pos]='*';        }        //cout<<pos<<endl;        if(pos>0)printf("LEFT\n");        pos--;    }    pos++;}int main(){    //freopen("cin.txt","r",stdin);    while(cin>>n>>k){        scanf("%s",s);        int pos=k-1;        if(2*k>n){ //right direction            right(pos);            left(pos);        }        else {  //left direction             left(pos);            right(pos);        }    }    return 0;}

B. Network Configuration

http://codeforces.com/problemset/problem/412/b

读懂它就能直接写

#include <iostream>#include <cstdio>#include <algorithm>using namespace std;int sp[110];int cmp(int a,int b){    return a>b;}int main(){    int n,k;    while(cin>>n>>k){        for(int i=0;i<n;i++){            scanf("%d",&sp[i]);        }        sort(sp,sp+n,cmp);        printf("%d\n",sp[k-1]);    }    return 0;}

C. Pattern

http://codeforces.com/problemset/problem/412/C
impelment (直接敲的题。)
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int N=1e5+10;char str[N],s[N];bool vis[N];  // ? --> alph --> ?int main(){    //freopen("cin.txt","r",stdin);    int n;    while(cin>>n){        scanf("%s",str);        memset(vis,0,sizeof(vis));        int len=strlen(str);        for(int i=1;i<n;i++){            scanf("%s",s);            for(int j=0;j<len;j++){                if(s[j]!='?'&&str[j]=='?'&&!vis[j]){                    str[j]=s[j];                }                if(s[j]!='?'&&str[j]!='?'){                    if(s[j]!=str[j]){                        str[j]='?';                        vis[j]=1;                    }                    else str[j]=s[j];                }            }        }        for(int i=0;i<len;i++){            if(!vis[i]&&str[i]=='?') str[i]='a';        }        printf("%s\n",str);    }    return 0;}
D题写在另一篇博客上:
http://blog.csdn.net/thearcticocean/article/details/50042735

E. E-mail Addresses
http://codeforces.com/contest/412/problem/E

将整个字符串扫描一遍。设整个串的中含有的address组成是:A @ B . C 那么问题结果就是 \sum A中的字母和(连续的前几个字母)与C中字母和(全部是字母)的乘积   【或者说是1 @ 2. 3】

细心细心,详细讨论各种情况,特别是A,B,C三种区域结束的讨论(@ 和 . 作为分隔符),意外结束和顺利结束所带来的影响也是不同的,意外结束都可能需要把当前区域字母个数作为A中的字母和(连续的前几个字母),继续下面的讨论。

#include <iostream>#include <cstdio>#include <cstring>using namespace std;typedef long long LL;const int N=1e6+10;char str[N];LL a,b,balp,c;int vis1,vis2,vis3;  //1 -> ok; -1 ->break; 0 ->not testbool check1(char ch){  // number    if((ch<58&&ch>=48)) return 1;    return 0;}bool check2(char ch){  // alpha    if(ch<123&&ch>=97) return 1;    return 0;}int main(){    //freopen("cin.txt","r",stdin);    while(~scanf("%s",str)){        int len=strlen(str);        LL sum=0;        for(int i=0;i<len;i++){            if(vis1!=1){ //1部分还没有结束                if(check1(str[i])==false&&check2(str[i])==false&&str[i]!='_'){                    if(str[i]=='@'&&a>0){                        vis1=1;                        continue;                    }                    else  a=0;                }                else {                    if(check2(str[i]))  a++;                }            }            if(vis1==1&&vis2==0){                if(str[i]=='.'&&b>0){                        vis2=1; // 通过测试                        continue;                }                else if(check1(str[i])||check2(str[i])) {                    if(check2(str[i])) balp++;                    b++;                }                else if(str[i]=='@'){                    a=balp;                    b=balp=0;                    if(a>0){                        vis1=1;                        continue;                    }                }                else {                    //vis2=-1;                    a=balp;                    b=balp=0;                    vis1=vis2=0;                    continue;                }            }            if(vis1==1&&vis2==1&&vis3==0){                if(check2(str[i])) c++;                else if(str[i]=='@'){                    sum=sum+a*c;                    a=c;                    vis1=1;                    b=c=0;                    balp=0;                    vis3=vis2=0;                }                else if(str[i]=='_' || check1(str[i])){                    sum=sum+a*c;                    vis1=vis2=vis3=0;                    a=c;                    b=c=0;                    balp=0;                }                else {                    //a=c;                    sum=sum+a*c;                    vis1=vis2=vis3=0;                    a=b=c=0;                    balp=0;                }            }            //printf("%d: %d %d %d\n",i,a,b,c);        }        if(a!=0&&c!=0){            sum=sum+a*c;            a=b=c=0;            balp=0;            vis1=vis2=vis3=0;        }        printf("%I64d\n",sum);    }    return 0;}



0 0
原创粉丝点击