栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 1728 Delta Encoding and D...

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 1728 Delta Encoding and D...

#include<iostream>#include<string>#include<vector>#include<stdio.h>#include <strings.h>using namespace std;enum {    ALP = 26,};int key[ALP];int rev[ALP];void outKey(){    for (int i=0; i<ALP; ++i){        printf("%c", 'A'+key[i]);    }}int getVal(char c){    if (isupper(c)){        return c-'A';    }    return c-'a';}inline int toVal(char c){    return rev[getVal(c)];}char enc(char p, char c){    if (!isalpha(c)) return c;    if (!isalpha(p)) p = 'a';    int t = toVal(c) - toVal(p);    t = (t + ALP)%ALP;    p = key[t] + 'a';    if (isupper(c)){        p = toupper(p);    }    return p;}char dec(char p, char c){    if (!isalpha(c)) return c;    if (!isalpha(p)) p = 'a';    int t = toVal(c) + toVal(p);    t = (t + ALP)%ALP;    p = key[t] + 'a';    if (isupper(c)){        p = toupper(p);    }    return p;}int changeKey(char *str = NULL){    int i;    if (str==0){        for (i=0; i<ALP; ++i){ key[i] = i; rev[i] = i;        }        return 0;    }    int j=0;    bool vis[ALP] = {false};    for (i=0; str[i]; ++i){        if (!isalpha(str[i])) continue;        int t = getVal(str[i]);        if (vis[t]){ changeKey(NULL); return -1;        }        vis[t] = true;        rev[t] = j;        key[j++] = t;    }    if (j<ALP){        changeKey(NULL);        return -1;    }    return 0;}char cmd[100];char line[10004];void encrypt(){    char p = 'a';    for (int i=0; line[i]; ++i){        char t = enc(p, line[i]);        p = line[i];        line[i] = t;    }    printf("RESULT: %s", line);}void decrypt(){    char p = 'a';    for (int i=0; line[i]; ++i){        line[i] = dec(p, line[i]);        p = line[i];    }    printf("RESULT: %s", line);}void fun(){    while(scanf("%s", cmd) > 0){        fgets(line, 10004, stdin);        if (strcasecmp(cmd, "ENCRYPT")==0){ encrypt();        } else if(strcasecmp(cmd, "DECRYPT")==0){ decrypt();        } else if(strcasecmp(cmd, "CIPHER")==0){ if (0==changeKey(line)){     printf("Good cipher.  Using ");     outKey();     printf(".n"); } else {     printf("Bad cipher.  Using default.n"); }        } else { printf("Command not understood.n");        }    }}int main(){    changeKey(0);    fun();    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/377156.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号