题解

36 条题解

  • 2
    @ 2017-10-08 21:39:19

    #include<iostream>
    using namespace std;
    char ch[26]={'0'};
    int main()
    {
    string str1,str2,str3;
    cin>>str1>>str2>>str3;//str1为密文,str2为明文,str3为需要翻译的密文
    int len1=str1.length(),len3=str3.length();//获取密文的长度
    if(len1<26)
    {
    cout<<"Failed";
    return 0;//若果密文的长度比26个字母长度短,就输出Failed,终止程序
    }
    for(int i=0;i<len1;i++)
    {
    for(int j=0;j<i;j++)
    {
    if(str2[i]==str2[j]&&str1[i]!=str1[j])
    {
    cout<<"Failed";//如果明文中有相同的字母但在密文中对应的字母不相同,程序错误输出Failed,终止程序
    return 0;
    }
    }
    ch[str1[i]-'A']=str2[i];
    }
    for(int i=0;i<len3;i++) cout<<ch[str3[i]-'A'];
    }

  • 2
    @ 2017-10-08 19:58:28

    #include<iostream>
    using namespace std;
    int main()
    {
    cout<<"Failed";
    }

  • 1
    @ 2018-03-30 09:01:13

    注意了,就算第3行里面需要的字母都能找到对应关系,也必须得把26个对应关系集齐了才能翻译,只要前两行没有集齐26个对应关系,就必须Failed
    #include <stdio.h>
    #include <string.h>

    int main()
    {
    char a[200],b[200],c[200],code[26];
    int i,lena,lenc,j;
    scanf("%s %s %s",a,b,c);
    lena=strlen(a);
    lenc=strlen(c);
    for(i=0;i<26;i++)
    code[i]=-1;
    for(i=0;i<lena;i++)
    {
    if(code[a[i]-'A']==-1)
    code[a[i]-'A']=b[i];
    else
    if(code[a[i]-'A']!=b[i])
    {
    printf("Failed");
    return 0;
    }
    }
    for(i=0;i<26;i++)
    for(j=i+1;j<26;j++)
    {
    if(code[i]==-1)
    {
    printf("Failed");
    return 0;
    }
    if(code[i]==-1&&code[j]==-1) continue;
    if(code[i]==code[j])
    {
    printf("Failed");
    return 0;
    }
    }
    for(i=0;i<lenc;i++)
    {
    if(code[c[i]-'A']==-1)
    {
    printf("Failed");
    return 0;
    }
    else
    c[i]=code[c[i]-'A'];
    }
    printf("%s",c);

    }

  • 1
    @ 2017-10-31 15:33:42

    一道简单的模拟题
    s1存的是给的加密串
    s2存的是给的原串
    s3是我们要求还原的字符串

    代码如下

    #include "iostream"
    #include <cstdio>
    #include <cstring>
    using namespace std;
    
    char ch[26] = {'0'};
    string s1,s2,s3;
    
    int main()
    {
        cin>>s1>>s2>>s3;
        int len1=s1.length(),len3=s3.length();
        if(len1 < 26)   cout<<"Failed"<<endl,exit(0);//给的长度不足以推断对应关系(不知道题是不是这个意思)
        for(int i=0;i<len1;i++)
            for(int j=0;j<i;j++)
                if(s2[i] == s2[j] && s1[i] != s1[j])    cout<<"Failed"<<endl,exit(0);//判断是否有冲突
        for(int i=0;i<len1;i++) ch[s1[i] - 'A'] = s2[i];//记录下对应关系(这一步其实可以和上一布合在一起写,我为了好看就拆开了)
        for(int i=0;i<len3;i++) printf("%c",ch[s3[i] - 'A']);
        return 0;
    }
    
  • 1
    @ 2017-10-01 08:29:56

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

    int lens,lene,tragf,e1[27],e2[27],eans[27];
    char ans[27];
    int main()
    {
    string s2,s1,ee;
    cin>>s1;
    cin>>s2;
    cin>>ee;
    lens=s1.length();
    lene=ee.length();
    for(int i=0;i<lens;i++)
    {
    e1[s1[i]-'A'+1]=1;
    e2[s2[i]-'A'+1]=1;
    if(ans[s1[i]-'A'+1]==0||(ans[s1[i]-'A'+1]==s2[i]))
    {
    ans[s1[i]-'A'+1]=s2[i];
    }
    else tragf=1;
    }
    if(!tragf)
    {
    for(int i=1;i<27;i++)
    {
    if(e1[i]==1&&e2[i]==0)tragf=1;
    if(ans[i]==0)tragf=1;
    }

    }
    if(tragf==1)
    {
    cout<<"Failed";
    }
    else for(int i=0;i<lene;i++)
    {
    cout<<ans[ee[i]-'A'+1];
    }

    return 0;
    }

  • 1
    @ 2017-08-23 01:28:59

    so water

    #include <iostream>
    #include <cstring>
    #include <cmath>
    int main() {
        char a[100],b[100],c[100],d[27]={0};
        int i=0,e=1,j;
        scanf("%s\n%s\n%s",a,b,c);
        int n=strlen(a)-1;
        int m=strlen(c)-1;
        for(i=0;i<=n;i++)
        {
            if(d[a[i]-64]==0||d[a[i]-64]==b[i])
            d[a[i]-64]=b[i];
            else
            e=0;
        }
        if(e!=0)
        for(i=1;i<=26;i++)
        {
            if(d[i]==0)
            {
                e=0;
                break;
            }
            else
            {
                for(j=i+1;j<=26;j++)
                {
                    if(d[i]==d[j])
                    {
                        e=0;
                        break;
                    }
                    if(e==0)
                    break;
                }
            }
        }
        if(e!=0)
        for(i=0;i<=m;i++)
        {
            if(d[c[i]-64]!=0)
            c[i]=d[c[i]-64];
            else
            e=0;
        }
        if(e==0)
        {
            printf("Failed");
        }
        if(e!=0)
        {
            for(i=0;i<=m;i++)
        {
            printf("%c",c[i]);
        }
        }
        return 0;
    }
    
  • 1
    @ 2016-11-13 13:28:26

    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <map>

    using namespace std;

    #define PRIN "Failed"
    //#define SCAN "spy"

    string a,b,c;
    map<char,char>mima;
    map<char,bool>mi;

    int main()
    {
    #ifdef SCAN
    freopen(SCAN".in","r",stdin);
    freopen(SCAN".out","w",stdout);
    #endif
    int i;
    int len;
    int k=0;
    cin>>a>>b>>c;
    len=a.size();
    for(i=0;i<len;++i)
    {
    if(mima.count(a[i])&&mima[a[i]]==b[i])continue;
    else if(mima.count(a[i])&&mima[a[i]]!=b[i])
    {
    cout<<PRIN;
    return 0;
    }
    else if(mi.count(b[i]))
    {
    cout<<PRIN;
    return 0;
    }else if(!mima.count(a[i]))
    {
    mima[a[i]]=b[i];
    mi[b[i]]=true;
    k++;
    }
    }
    if(k!=26)
    cout<<PRIN;
    else
    {
    len=c.size();
    for(i=0;i<len;++i)
    cout<<mima[c[i]];
    }
    return 0;
    }

  • 1
    @ 2016-10-27 19:31:59

    #include<stdio.h>
    #include<stdlib.h>
    #include<iostream>
    #include<algorithm>
    #include<string.h>
    #include<queue>
    using namespace std;
    char y[105],m[105],a[105],o[105];
    int y_[105],m_[105],a_[105],z[36],used[36];
    int main()
    {
    //freopen("spy.in","r",stdin);
    //freopen("spy.out","w",stdout);
    int i,leny,lenm,lena,flag=0,ans=0;
    cin>>m>>y>>a;
    leny=strlen(y);
    lenm=strlen(m);
    lena=strlen(a);
    for(i=0;i<leny;i++)
    {
    y_[i]=(y[i]-'A')+1;
    if(used[y_[i]]==0)
    {
    used[y_[i]]=1;
    ans++;
    }
    }
    if(ans!=26)
    flag=1;
    memset(used,0,sizeof(used));
    ans=0;
    for(i=0;i<lenm;i++)
    {
    m_[i]=(m[i]-'A')+1;
    if(used[m_[i]]==0)
    {
    used[m_[i]]=1;
    ans++;
    }
    }
    if(ans!=26)
    flag=1;
    for(i=0;i<lena;i++)
    a_[i]=(a[i]-'A')+1;
    for(i=0;i<leny;i++)
    {
    if(z[m_[i]]!=0&&z[m_[i]]!=y_[i])
    {
    flag=1;
    break;
    }
    if(z[m_[i]]==0)
    {
    z[m_[i]]=y_[i];
    }

    }
    for(i=1;i<=26;i++)
    if(z[i]==0)
    flag=1;
    if(flag==1)
    {
    cout<<"Failed"<<endl;
    }
    else
    {
    for(i=0;i<lena;i++)
    a_[i]=z[a_[i]];
    for(i=0;i<lena;i++)
    o[i]=((a_[i]-1)+'A');
    cout<<o<<endl;
    }
    return 0;
    }

  • 1
    @ 2016-10-08 16:58:39
    #include<cstring>
    #include<iostream>
    using namespace std;
    
    int Num;
    string Map, Ori, New;
    char LinkM[26], LinkO[26];
    
    int main()
    {
        cin >> Map >> Ori >> New;
        for (int i = 0; i < Map.size(); ++i)
        {
            if (!LinkM[Ori[i]-'A'] && !LinkO[Map[i]-'A'] && ++Num)
            {
                LinkM[Ori[i]-'A'] = Map[i];
                LinkO[Map[i]-'A'] = Ori[i];
            }
            else    if (LinkM[Ori[i]-'A'] != Map[i] || LinkO[Map[i]-'A'] != Ori[i])
                return cout << "Failed", 0;
        }
        if (Num != 26)  return cout << "Failed", 0;
        for (int i = 0; i < New.size(); ++i)    cout << LinkO[New[i]-'A'];
        return 0;
    }
    
  • 1
    @ 2015-09-30 20:51:45

    /*

    Author : Slience_K
    Belong : C++
    Pro : NOIP 2009 - 1

    */
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    char s[ 105 ] , m[ 105 ] , t[ 105 ] , a[ 'Z' + 1 ] = {} , b[ 'Z' + 1 ] = {};
    int len;
    int main(){
    //freopen( "spy.in" , "r" , stdin );
    //freopen( "spy.out" , "w" , stdout );
    memset( a , ' ' , sizeof( a ) );
    memset( b , ' ' , sizeof( b ) );
    scanf( "%s%s" , &m , &s );
    len = strlen( s );
    for( int i = 0 ; i < len ; i++ ){
    if (( a[ s[ i ] ] == ' ' )&&( b[ m[ i ] ] == ' ' )){
    a[ s[ i ] ] = m[ i ];
    b[ m[ i ] ] = s[ i ];
    continue;
    }
    if (( a[ s[ i ] ] == m[ i ] )&&( b[ m[ i ] ] == s[ i ] )) continue;
    printf( "Failed" );
    //fclose( stdin );
    // fclose( stdout );
    return 0;
    }
    for( char i = 'A' ; i <= 'Z' ; i++ )
    if ( b[ i ] == ' ' ){
    printf( "Failed" );
    // fclose( stdin );
    // fclose( stdout );
    return 0;
    }
    scanf( "%s" , &t );
    len = strlen( t );
    for( int i = 0 ; i < len ; i++ )
    printf( "%c" , b[ t[ i ] ] );
    //fclose( stdin );
    //fclose( stdout );
    return 0;

    }

  • 0
    @ 2016-08-29 13:37:11

    var
    s1,s2,s3,s:string;
    i,j,n,m:longint;
    c:array[1..100000]of char;
    begin
    readln(s1);
    readln(s2);
    readln(s3);
    n:=length(s1);s:='';s:=s+s1;m:=length(s1);
    for i:=1 to m-1 do
    for j:=m downto i+1 do
    if (s[i]=s[j]) then begin dec(m); delete(s,j,1); end;
    if m<>26 then begin writeln('Failed'); halt; end;
    for i:=1 to n-1 do
    for j:=i+1 to n do
    if ((s1[i]=s1[j]) and (s2[i]<>s2[j])) or ((s2[i]=s2[j]) and (s1[i]<>s1[j])) then
    begin writeln('Failed'); halt; end;
    for i:=1 to length(s3) do
    for j:=1 to n do
    if s3[i]=s1[j] then c[i]:=s2[j];
    for i:=1 to length(s3) do
    write(c[i]);writeln;
    end.
    请大神帮忙缩减一下

  • 0
    @ 2016-06-09 11:22:25

    #include <cstdio>
    #include <cstring>

    int main(){
    #ifdef DEBUG
    freopen("in.txt","r",stdin);
    #endif
    char a[200],b[200],x[200];
    int key[200]={0},refer[200]={0};
    int flag=0;
    scanf("%s%s%s",a,b,x); //a为加密后 b为原文
    for(int i=0;i<strlen(a);i++){
    int m=b[i],n=a[i];
    if(key[n]!=0&&key[n]!=m){
    flag=1;
    break;
    }
    if(refer[m]!=0&&refer[m]!=n){
    flag=1;
    break;
    }
    key[n]=m;
    refer[m]=n;
    }
    for(int i='A';i<='Z';i++)
    flag+=key[i]==0;
    if(flag){
    printf("Failed");
    return 0;
    }
    for(int i=0;i<strlen(x);i++)
    printf("%c",key[x[i]]);
    return 0;
    }

  • 0
    @ 2015-11-05 14:58:25

    比较简单的模拟 看清楚题目即可
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    char mi[110],yuan[110], f[110],f2[110],goal[110];
    int cnt;
    int main(){

    scanf("%s%s%s",mi,yuan,goal);
    int len1=strlen(mi),len3=strlen(goal);
    for(int i=0;i<len1;i++){
    if( (!f[mi[i]] && !f2[yuan[i]]) )f[mi[i]]=yuan[i],f2[yuan[i]]=mi[i],cnt++;
    else if( f[mi[i]]==yuan[i] && f2[yuan[i]]) continue;
    else {printf("Failed");exit(0);
    }

    }
    if(cnt!=26) {printf("Failed");exit(0);}
    for(int i=0;i<len3;i++) goal[i]=f[goal[i]];
    printf("%s",goal);
    return 0;
    }

  • 0
    @ 2015-10-22 20:14:55

    简单的模拟题,要注意题目。原码和编码要一一对应,编码和原码反过来也要一一对应。

    比如QQ 显然不合法 同样 AB 也不合法。另外要注意是否所有字母都有对应。之后模拟即可。
    AB QQ

    给出如下参考代码。
    ###pascal code
    program P1752;
    var a:array['A'..'Z'] of char;
    pd,pd2:array['A'..'Z'] of boolean;
    s1,s2,s3:string;
    i,j:longint;
    k:char;
    ok:boolean;
    begin
    readln(s1); readln(s2); readln(s3); ok:=true; fillchar(pd,sizeof(pd),false);
    fillchar(pd2,sizeof(pd2),false);
    for k:='A' to 'Z' do a[k]:='?'; //一开始什么字符都不知道
    for i:=1 to length(s1) do
    begin
    if ((a[s1[i]]='?') and (pd[s1[i]]=false)) or (a[s1[i]]=s2[i]) then //记录对应字符。对于已经有对应的字符如果又出现
    begin //一个新的字符与其对应说明fail
    a[s1[i]]:=s2[i]; pd[s1[i]]:=true;
    end
    else
    begin
    ok:=false; break;
    end;
    end;

    if ok=false then begin write('Failed'); exit; end;

    for k:='A' to 'Z' do //还有没有对应的字符,fail
    if a[k]='?' then
    begin
    write('Failed'); exit;
    end;

    for k:='A' to 'Z' do //对应了多个编码,fail
    if pd2[a[k]]=false then
    begin
    pd2[a[k]]:=true;
    end
    else
    begin
    write('Failed'); exit;
    end;

    for i:=1 to length(s3) do //有答案,输出
    write(a[s3[i]]);

    end.

  • 0
    @ 2015-09-30 23:17:01

    AC100
    C语言

    #include <stdio.h>

    char s1[100],s2[100],s[100];
    int pwd[27]={0};

    int main(){
    scanf("%s%s%s",&s1,&s2,&s);
    int i,temp1,temp2;
    for(i=1;i<=100;i++){
    if(s1[i-1]=='\0')
    break;
    temp1=s1[i-1]-'A'+1;
    temp2=s2[i-1]-'A'+1;
    if(pwd[temp1]==0){
    pwd[temp1]=temp2;
    }
    else
    if(pwd[temp1]!=0&&pwd[temp1]!=temp2){
    printf("Failed");
    return 0;
    }
    }

    int flag[27]={0};

    for(i=1;i<=26;i++){
    if(pwd[i]!=0)
    flag[pwd[i]]++;
    if(pwd[i]==0){
    printf("Failed");
    return 0;
    }
    }

    for(i=1;i<=26;i++)
    if(flag[i]>1){
    printf("Failed");
    return 0;
    }

    for(i=0;i<100;i++){
    if(s[i]=='\0')
    break;
    temp1=s[i]-'A'+1;
    temp2=pwd[temp1];
    printf("%c",temp2+'A'-1);
    }

    return 0;
    }

  • 0
    @ 2015-08-18 21:58:32

    var
    m,k,n:string;
    i:longint;
    j:char;
    a1,a2:array['A'..'Z'] of char;
    begin
    readln(m);
    readln(n);
    readln(k);
    for j:='A' to 'Z' do
    begin a1[j]:='0';a2[j]:='0';end;
    for i:=1 to length(m) do
    begin
    if (a1[m[i]]<>'0') and (a1[m[i]]<>n[i]) then
    begin
    writeln('Failed');
    close(input);
    close(output);
    halt;
    end;
    if (a2[n[i]]<>'0') and (a2[n[i]]<>m[i]) then
    begin
    writeln('Failed');
    halt;
    end;
    a1[m[i]]:=n[i];a2[n[i]]:=m[i];
    end;
    for j:='A' to 'Z' do
    if a1[j]='0' then
    begin
    writeln('Failed');
    halt;
    end;
    for i:=1 to length(k) do
    write(a1[k[i]]);
    end.

  • 0
    @ 2015-03-21 20:08:40

    仔细读题,想清楚题意再做。

    Pascal Code

    var
    a,b,c:string;
    p:array['A'..'Z'] of char; //下标为密文,值为原文
    hash:array['A'..'Z'] of boolean; //下标为原文,值表示这一原文有没有使用过
    i:longint;
    j:char;
    procedure fail;
    begin
    writeln('Failed');
    halt;
    end;
    begin
    readln(a);
    readln(b);
    readln(c);
    fillchar(p,sizeof(p),' ');
    fillchar(hash,sizeof(hash),0);
    if length(a)<>length(b) then fail;
    for i:=1 to length(a) do
    begin
    if (p[a[i]]<>b[i]) and (p[a[i]]<>' ') then fail;
    p[a[i]]:=b[i];
    end;
    for j:='A' to 'Z' do
    begin
    if (p[j]=' ') or hash[p[j]] then fail;
    hash[p[j]]:=true;
    end;
    for i:=1 to length(c) do
    write(p[c[i]]);
    end.

  • 0
    @ 2015-01-18 17:06:06

    #include<iostream>
    #include<string.h>
    using namespace std;
    int main()
    {
    string code, text, ncode, ntext;
    bool check = true, chr[26];
    char match[26];

    cin >> code >> text >> ncode;
    memset(chr,false,sizeof(chr));

    int len = code.length();
    int nlen = ncode.length();

    int i, j;
    //情况2

    if(check)
    {
    for(i = 0; i < len-1; i++)
    for(j = i+1; j < len; j++)
    {
    if((text[i] != text[j]) && (code[i] == code[j]))
    check = false;
    }

    }

    //情况3

    if(check)
    {
    for(i = 0; i < len; i++)
    chr[(int)text[i]-65] = true;
    for(i = 0; i < 26; i++)
    if(chr[i] == false) check = false;
    }

    if(check == false)
    {
    cout << "Failed" << endl;
    }
    else
    {
    for(i = 0; i < len; i++) //建立已知 密文 与 明文对应关系
    match[(int)code[i]-65] = text[i];
    for(i = 0; i < nlen; i++)
    cout << match[(int)ncode[i]-65];
    cout << endl;
    }

    return 0;
    }

  • 0
    @ 2014-11-06 19:28:10

    测试数据 #0: Accepted, time = 0 ms, mem = 508 KiB, score = 10
    测试数据 #1: Accepted, time = 0 ms, mem = 508 KiB, score = 10
    测试数据 #2: Accepted, time = 0 ms, mem = 508 KiB, score = 10
    测试数据 #3: Accepted, time = 0 ms, mem = 512 KiB, score = 10
    测试数据 #4: Accepted, time = 15 ms, mem = 504 KiB, score = 10
    测试数据 #5: Accepted, time = 15 ms, mem = 508 KiB, score = 10
    测试数据 #6: Accepted, time = 0 ms, mem = 508 KiB, score = 10
    测试数据 #7: Accepted, time = 15 ms, mem = 508 KiB, score = 10
    测试数据 #8: Accepted, time = 15 ms, mem = 508 KiB, score = 10
    测试数据 #9: Accepted, time = 0 ms, mem = 508 KiB, score = 10
    Accepted, time = 60 ms, mem = 512 KiB, score = 100
    代码
    #include <cstdio>
    #include <map>
    #include <string>
    #include <iostream>
    using namespace std;
    #define M 10000

    int ssize;
    string origin,sec,trans;
    map<char,char> dict;

    int main() {
    cin>>sec>>origin>>trans;
    ssize = sec.size();
    if(sec.size()<26) {
    printf("Failed\n");
    return 0;
    }
    if(sec.size()==26) {
    for(int i = 0; i < ssize; i++) {
    for(int j = 0; j < ssize&&i!=j; j++) {
    if(sec[i]==sec[j]||origin[i]==origin[j]) {
    printf("Failed\n");
    return 0;
    }
    }
    }
    }
    dict[sec[0]] = origin[0];
    for(int i = 1; i < ssize; i++) {
    if(dict[sec[i]]!=NULL && dict[sec[i]]!=origin[i]) {
    printf("Failed\n");
    return 0;
    }
    dict[sec[i]] = origin[i];
    }
    for(int i = 0; i < trans.size(); i++) {
    cout<<dict[trans[i]];
    }
    cout<<endl;
    return 0;
    }

信息

ID
1752
难度
5
分类
模拟 点击显示
标签
递交数
3054
已通过
994
通过率
33%
上传者