有一种加密方法为:其使用一个字母串(可以含重复字母,字母个数不超过50)作为密钥。假定密钥单词串为feather,则先去掉密钥单词中的重复字母得到单词串feathr,然后将其反序,并将字母表中的其它字母以反序追加到后面:
r h t a e f z y x w v u s q p o n m l k j i g d c b 加密字母的对应关系如下:
a b c d e f g h i j k l m n o p q r s t u v w x y z r h t a e f z y x w v u s q p o n m l k j i g d c b 其中第一行为原始英文字母,第二行为对应加密字母。假定输入密钥全为小写字母。加密时若原始字符为英文大小写字母,则可以由上述对应关系找到加密后的字母,且要求加密前后字母的大小写相同,例如字符"n"加密后变为"q",字符"N"加密后变为"Q"。其它字符不进行加密。编写一个程序,用这种密码加密输入的字符串。
【输入形式】从标准输入中输入密钥串,然后在下一行输入要加密的字符串。密钥串字母个数不超过50个,待加密字符串的字符数不超过100个。 【输出形式】加密后结果输出到标准输出。 【样例输入】 feather C Language Is Wonderful.
【样例输出】 T Urqzjrze Xl Gpqaemfju.
【样例说明】首先将给定的密钥单词去除重复字母并反序,然后按照上面的加密对应表对后面的内容进行加密即可得到加密后的字符串,其中只对英文字母进行加密转换。
【评分标准】该题要求对输入的字符串进行加密。提交程序名为encrypt.c或encrypt.cpp。
emm,这一题难点大概在字符串翻转?还是决定一个好的存储方式比较重要。存储字符串,可以采用C风格的字符数组,但是后续的翻转、去重将非常复杂。在C++中有一个字符串类叫string,具有很多便捷的操作,因此在这里使用string存储所有的字符串。
这一题的解答可以分为两步:
#mermaid-svg-F6UxP71Utdodq2dP .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-F6UxP71Utdodq2dP .label text{fill:#333}#mermaid-svg-F6UxP71Utdodq2dP .node rect,#mermaid-svg-F6UxP71Utdodq2dP .node circle,#mermaid-svg-F6UxP71Utdodq2dP .node ellipse,#mermaid-svg-F6UxP71Utdodq2dP .node polygon,#mermaid-svg-F6UxP71Utdodq2dP .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-F6UxP71Utdodq2dP .node .label{text-align:center;fill:#333}#mermaid-svg-F6UxP71Utdodq2dP .node.clickable{cursor:pointer}#mermaid-svg-F6UxP71Utdodq2dP .arrowheadPath{fill:#333}#mermaid-svg-F6UxP71Utdodq2dP .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-F6UxP71Utdodq2dP .flowchart-link{stroke:#333;fill:none}#mermaid-svg-F6UxP71Utdodq2dP .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-F6UxP71Utdodq2dP .edgeLabel rect{opacity:0.9}#mermaid-svg-F6UxP71Utdodq2dP .edgeLabel span{color:#333}#mermaid-svg-F6UxP71Utdodq2dP .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-F6UxP71Utdodq2dP .cluster text{fill:#333}#mermaid-svg-F6UxP71Utdodq2dP div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-F6UxP71Utdodq2dP .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-F6UxP71Utdodq2dP text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-F6UxP71Utdodq2dP .actor-line{stroke:grey}#mermaid-svg-F6UxP71Utdodq2dP .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-F6UxP71Utdodq2dP .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-F6UxP71Utdodq2dP #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-F6UxP71Utdodq2dP .sequenceNumber{fill:#fff}#mermaid-svg-F6UxP71Utdodq2dP #sequencenumber{fill:#333}#mermaid-svg-F6UxP71Utdodq2dP #crosshead path{fill:#333;stroke:#333}#mermaid-svg-F6UxP71Utdodq2dP .messageText{fill:#333;stroke:#333}#mermaid-svg-F6UxP71Utdodq2dP .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-F6UxP71Utdodq2dP .labelText,#mermaid-svg-F6UxP71Utdodq2dP .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-F6UxP71Utdodq2dP .loopText,#mermaid-svg-F6UxP71Utdodq2dP .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-F6UxP71Utdodq2dP .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-F6UxP71Utdodq2dP .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-F6UxP71Utdodq2dP .noteText,#mermaid-svg-F6UxP71Utdodq2dP .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-F6UxP71Utdodq2dP .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-F6UxP71Utdodq2dP .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-F6UxP71Utdodq2dP .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-F6UxP71Utdodq2dP .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-F6UxP71Utdodq2dP .section{stroke:none;opacity:0.2}#mermaid-svg-F6UxP71Utdodq2dP .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-F6UxP71Utdodq2dP .section2{fill:#fff400}#mermaid-svg-F6UxP71Utdodq2dP .section1,#mermaid-svg-F6UxP71Utdodq2dP .section3{fill:#fff;opacity:0.2}#mermaid-svg-F6UxP71Utdodq2dP .sectionTitle0{fill:#333}#mermaid-svg-F6UxP71Utdodq2dP .sectionTitle1{fill:#333}#mermaid-svg-F6UxP71Utdodq2dP .sectionTitle2{fill:#333}#mermaid-svg-F6UxP71Utdodq2dP .sectionTitle3{fill:#333}#mermaid-svg-F6UxP71Utdodq2dP .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-F6UxP71Utdodq2dP .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-F6UxP71Utdodq2dP .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-F6UxP71Utdodq2dP .grid path{stroke-width:0}#mermaid-svg-F6UxP71Utdodq2dP .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-F6UxP71Utdodq2dP .task{stroke-width:2}#mermaid-svg-F6UxP71Utdodq2dP .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-F6UxP71Utdodq2dP .taskText:not([font-size]){font-size:11px}#mermaid-svg-F6UxP71Utdodq2dP .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-F6UxP71Utdodq2dP .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-F6UxP71Utdodq2dP .task.clickable{cursor:pointer}#mermaid-svg-F6UxP71Utdodq2dP .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-F6UxP71Utdodq2dP .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-F6UxP71Utdodq2dP .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-F6UxP71Utdodq2dP .taskText0,#mermaid-svg-F6UxP71Utdodq2dP .taskText1,#mermaid-svg-F6UxP71Utdodq2dP .taskText2,#mermaid-svg-F6UxP71Utdodq2dP .taskText3{fill:#fff}#mermaid-svg-F6UxP71Utdodq2dP .task0,#mermaid-svg-F6UxP71Utdodq2dP .task1,#mermaid-svg-F6UxP71Utdodq2dP .task2,#mermaid-svg-F6UxP71Utdodq2dP .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-F6UxP71Utdodq2dP .taskTextOutside0,#mermaid-svg-F6UxP71Utdodq2dP .taskTextOutside2{fill:#000}#mermaid-svg-F6UxP71Utdodq2dP .taskTextOutside1,#mermaid-svg-F6UxP71Utdodq2dP .taskTextOutside3{fill:#000}#mermaid-svg-F6UxP71Utdodq2dP .active0,#mermaid-svg-F6UxP71Utdodq2dP .active1,#mermaid-svg-F6UxP71Utdodq2dP .active2,#mermaid-svg-F6UxP71Utdodq2dP .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-F6UxP71Utdodq2dP .activeText0,#mermaid-svg-F6UxP71Utdodq2dP .activeText1,#mermaid-svg-F6UxP71Utdodq2dP .activeText2,#mermaid-svg-F6UxP71Utdodq2dP .activeText3{fill:#000 !important}#mermaid-svg-F6UxP71Utdodq2dP .done0,#mermaid-svg-F6UxP71Utdodq2dP .done1,#mermaid-svg-F6UxP71Utdodq2dP .done2,#mermaid-svg-F6UxP71Utdodq2dP .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-F6UxP71Utdodq2dP .doneText0,#mermaid-svg-F6UxP71Utdodq2dP .doneText1,#mermaid-svg-F6UxP71Utdodq2dP .doneText2,#mermaid-svg-F6UxP71Utdodq2dP .doneText3{fill:#000 !important}#mermaid-svg-F6UxP71Utdodq2dP .crit0,#mermaid-svg-F6UxP71Utdodq2dP .crit1,#mermaid-svg-F6UxP71Utdodq2dP .crit2,#mermaid-svg-F6UxP71Utdodq2dP .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-F6UxP71Utdodq2dP .activeCrit0,#mermaid-svg-F6UxP71Utdodq2dP .activeCrit1,#mermaid-svg-F6UxP71Utdodq2dP .activeCrit2,#mermaid-svg-F6UxP71Utdodq2dP .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-F6UxP71Utdodq2dP .doneCrit0,#mermaid-svg-F6UxP71Utdodq2dP .doneCrit1,#mermaid-svg-F6UxP71Utdodq2dP .doneCrit2,#mermaid-svg-F6UxP71Utdodq2dP .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-F6UxP71Utdodq2dP .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-F6UxP71Utdodq2dP .milestoneText{font-style:italic}#mermaid-svg-F6UxP71Utdodq2dP .doneCritText0,#mermaid-svg-F6UxP71Utdodq2dP .doneCritText1,#mermaid-svg-F6UxP71Utdodq2dP .doneCritText2,#mermaid-svg-F6UxP71Utdodq2dP .doneCritText3{fill:#000 !important}#mermaid-svg-F6UxP71Utdodq2dP .activeCritText0,#mermaid-svg-F6UxP71Utdodq2dP .activeCritText1,#mermaid-svg-F6UxP71Utdodq2dP .activeCritText2,#mermaid-svg-F6UxP71Utdodq2dP .activeCritText3{fill:#000 !important}#mermaid-svg-F6UxP71Utdodq2dP .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-F6UxP71Utdodq2dP g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-F6UxP71Utdodq2dP g.classGroup text .title{font-weight:bolder}#mermaid-svg-F6UxP71Utdodq2dP g.clickable{cursor:pointer}#mermaid-svg-F6UxP71Utdodq2dP g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-F6UxP71Utdodq2dP g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-F6UxP71Utdodq2dP .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-F6UxP71Utdodq2dP .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-F6UxP71Utdodq2dP .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-F6UxP71Utdodq2dP .dashed-line{stroke-dasharray:3}#mermaid-svg-F6UxP71Utdodq2dP #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-F6UxP71Utdodq2dP #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-F6UxP71Utdodq2dP #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-F6UxP71Utdodq2dP #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-F6UxP71Utdodq2dP #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-F6UxP71Utdodq2dP #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-F6UxP71Utdodq2dP #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-F6UxP71Utdodq2dP #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-F6UxP71Utdodq2dP .commit-id,#mermaid-svg-F6UxP71Utdodq2dP .commit-msg,#mermaid-svg-F6UxP71Utdodq2dP .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-F6UxP71Utdodq2dP .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-F6UxP71Utdodq2dP .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-F6UxP71Utdodq2dP g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-F6UxP71Utdodq2dP g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-F6UxP71Utdodq2dP g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-F6UxP71Utdodq2dP g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-F6UxP71Utdodq2dP g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-F6UxP71Utdodq2dP g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-F6UxP71Utdodq2dP .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-F6UxP71Utdodq2dP .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-F6UxP71Utdodq2dP .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-F6UxP71Utdodq2dP .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-F6UxP71Utdodq2dP .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-F6UxP71Utdodq2dP .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-F6UxP71Utdodq2dP .edgeLabel text{fill:#333}#mermaid-svg-F6UxP71Utdodq2dP .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-F6UxP71Utdodq2dP .node circle.state-start{fill:black;stroke:black}#mermaid-svg-F6UxP71Utdodq2dP .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-F6UxP71Utdodq2dP #statediagram-barbEnd{fill:#9370db}#mermaid-svg-F6UxP71Utdodq2dP .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-F6UxP71Utdodq2dP .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-F6UxP71Utdodq2dP .statediagram-state .divider{stroke:#9370db}#mermaid-svg-F6UxP71Utdodq2dP .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-F6UxP71Utdodq2dP .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-F6UxP71Utdodq2dP .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-F6UxP71Utdodq2dP .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-F6UxP71Utdodq2dP .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-F6UxP71Utdodq2dP .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-F6UxP71Utdodq2dP .note-edge{stroke-dasharray:5}#mermaid-svg-F6UxP71Utdodq2dP .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-F6UxP71Utdodq2dP .error-icon{fill:#522}#mermaid-svg-F6UxP71Utdodq2dP .error-text{fill:#522;stroke:#522}#mermaid-svg-F6UxP71Utdodq2dP .edge-thickness-normal{stroke-width:2px}#mermaid-svg-F6UxP71Utdodq2dP .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-F6UxP71Utdodq2dP .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-F6UxP71Utdodq2dP .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-F6UxP71Utdodq2dP .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-F6UxP71Utdodq2dP .marker{fill:#333}#mermaid-svg-F6UxP71Utdodq2dP .marker.cross{stroke:#333} :root { --mermaid-font-family: "trebuchet ms", verdana, arial;} #mermaid-svg-F6UxP71Utdodq2dP { color: rgba(0, 0, 0, 0.75); font: ; } 构造加密字典 加密由于字母表是固定的,加密字典的构造只和加密密钥有关,这里构造的时候可以直接逆序。
int j; for(j=0;j<key.size();j++){//key为密钥,dict为待构造的字典 if(dict.find(key[j])==dict.npos){//这里是string类的一个查找函数,npos即未查到 dict=key[j]+dict;//将该字符插入在字典最前面,达到逆序的目的 } } for(j=122;j>=97;j--){//将字母表中的数逆序存入字典中 if(dict.find(j)==dict.npos){ dict+=j; } }到这一步加密字典就构建成功了,接着只需对字符串进行加密即可。由于原来的加密字典中的字符对应的是从a-z的加密值,因此可以直接通过待加密字符相对a的位置得到加密值。
char secret(char x,string dict){ if(x<97){//若为大写字母 return dict[x+32-97]-32; } else{//若为小写字母 return dict[x-97]; } }由于其他特殊字符在加密前做了判断,这里就不再显示啦!