codeforces 412 impelment、greedy

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

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 test
bool 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值