UVaOJ 112道题目-字符串

1、110301/10082 WERTYU (WERTYU 键盘)

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<ctype.h>
using namespace std;
char key[4][20]={"`1234567890-=","QWERTYUIOP[]\","ASDFGHJKL;'","ZXCVBNM,./"};
char cmd[100000];
struct node
{
    int x,y;
}asc[300];
int main()
{
    int i,j,x,y;
    char c;
    for(i=0;i<4;i++)
    {
        for(j=0;key[i][j]!='';j++)
        {
            c=key[i][j];
            asc[c].x=i;
            asc[c].y=j;
        }
    }
    while(gets(cmd)!=NULL)
    {
        for(i=0;cmd[i]!='';i++)
        {
            c=cmd[i];
            if(c==' ')
            {
                printf(" ");
                continue;
            }
            x=asc[c].x;
            y=asc[c].y;
            if(y==0)
                printf("%c",cmd[i]);
            else
            {
                c=key[x][y-1];
                printf("%c",c);
            }
        }
        printf("
");
    }
    return 0;
}
View Code

 2、110302/10010 Where’s Waldorf?(寻找单词)

使用方向数组

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<ctype.h>
using namespace std;
char str[100][100];
char word[100];
int dis[8][2]=
{
    {-1,0},
    {1,0},
    {0,-1},
    {0,1},
    {-1,-1},
    {-1,1},
    {1,-1},
    {1,1}
};
void Trans(char str[])
{
    int i;
    for(i=0;str[i]!='';i++)
    {
        if(str[i]>='A'&&str[i]<='Z')str[i]=str[i]-'A'+'a';
    }
}
bool Verify(int x,int y,int m,int n)
{
    if(x>m||x<0)return false;
    if(y>n||y<0)return false;
    return true;
}
void Search(int m,int n)
{
    int i,j,k,t,x,y;
    int len=strlen(word);
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            for(k=0;k<8;k++)
            {
                int t=0;
                x=i;
                y=j;
                while(t<len)
                {
                    if(word[t]!=str[x][y])break;
                    t++;
                    x+=dis[k][0];
                    y+=dis[k][1];
                    if(!Verify(x,y,m,n))break;
                }
                if(t==len)
                {
                    printf("%d %d
",i+1,j+1);
                    return;
                }
            }
        }
    }
}
int main()
{
    int T,m,n,k,i,j;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&m,&n);
        for(i=0;i<m;i++)
        {
            scanf("%s",str[i]);
            Trans(str[i]);
        }
        scanf("%d",&k);
        for(i=0;i<k;i++)
        {
            scanf("%s",word);
            Trans(word);
            Search(m,n);
        }
        if(T)printf("
");
    }
    return 0;
}
View Code

 3、110303/10252 Common Permutation (公共排列)

可以计算每个字母出现的次数求解

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<ctype.h>
using namespace std;
char sta[1005],stb[1005];
int a[100],b[100];
void calc(char str[],int num[])
{
    int i;
    for(i=0;str[i]!='';i++)num[str[i]-'a']++;
}
int main()
{
    while(gets(sta)!=NULL)
    {
        int i,j;
        gets(stb);
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        calc(sta,a);
        calc(stb,b);
        for(i=0;i<26;i++)
        {
            for(j=0;j<a[i]&&j<b[i];j++)printf("%c",'a'+i);
        }
        printf("
");
    }
    return 0;
}
View Code

 4、110304/850  Crypt Kicker II (解密 II)

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<ctype.h>
using namespace std;
char txt[105][105];
char ent[100]="the quick brown fox jumps over the lazy dog";
int len[105];
void crypt(int xl,int alp[],int vis[])
{
    int i,j,cnt;
    for(cnt=0;cnt<xl;cnt++)
    {
        for(i=0;i<len[cnt];i++)
        {
            if(txt[cnt][i]==' ')continue;
            int a=txt[cnt][i]-'a';
            if(alp[a]==-1)
            {
                for(j=0;j<26;j++)
                {
                    if(vis[j]==0)
                    {
                        vis[j]=1;
                        alp[a]=j;
                    }
                }
            }
        }    
    }
}
void input()
{
    int i=0,j,t,k;
    int alp[100],vis[100];
    while(gets(txt[i])!=NULL)
    {
        len[i]=strlen(txt[i]);
        if(len[i]==0)break;
        i++;
    }
    int xl=i,el=strlen(ent);
    for(i=0;i<xl;i++)
    {
        if(len[i]==el)
        {
            memset(alp,-1,sizeof(alp));
            memset(vis,0,sizeof(vis));
            for(j=0;j<len[i];j++)
            {
                if(txt[i][j]==' '&&ent[j]==' ')continue;
                else if(txt[i][j]==' '&&ent[j]!=' ')break;
                else if(txt[i][j]!=' '&&ent[j]==' ')break;
                int c=txt[i][j]-'a';
                int b=ent[j]-'a';
                if(alp[c]!=b&&alp[c]==-1)
                {
                    int b=ent[j]-'a';
                    if(vis[b]!=0)break;
                    vis[b]=-1;
                    alp[c]=b;
                }
                if(alp[c]!=b&&alp[c]!=-1)break;
            }
            if(j>=len[i])
            {
                crypt(xl,alp,vis);
                for(t=0;t<xl;t++)
                {
                    for(k=0;k<len[t];k++)
                    {
                        if(txt[t][k]==' ')
                        {
                            printf(" ");
                            continue;
                        }
                        int a=txt[t][k]-'a';
                        printf("%c",alp[a]+'a');
                    }
                    printf("
");
                }
                return ;
            }
        }
    }
    printf("No solution.
");
}
int main()
{
    int T;
    scanf("%d",&T);
    getchar();
    gets(txt[0]);
    while(T--)
    {
        input();
        if(T)printf("
");
    }
    return 0;
}
View Code

 5、110305/10188 Automated Judge Script (自动评测脚本)

每行中的数字需要一起比较

用例:

2

1 0

5

2

1

0 5

Presentation Error
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<ctype.h>
using namespace std;
char astd[105][150],ans[105][150];
char ns[15000],na[15000];
char res[3][20]={"Accepted","Presentation Error","Wrong Answer"};
int main()
{
    int n,m,i,j,t=0;
    int T=1;
    while(scanf("%d",&n)!=EOF&&n)
    {
        getchar();
        int match=0;
        t=0;
        for(i=0;i<n;i++)
        {
            gets(astd[i]);
            for(j=0;astd[i][j]!='';j++)
            {
                if(isdigit(astd[i][j]))ns[t++]=astd[i][j];
            }
        }
        ns[t]='';
        scanf("%d",&m);
        getchar();
        t=0;
        for(i=0;i<m;i++)
        {
            gets(ans[i]);
            for(j=0;ans[i][j]!='';j++)
            {
                if(isdigit(ans[i][j]))na[t++]=ans[i][j];
            }
        }
        na[t]='';
        if(m==n)
        {
            for(i=0;i<m;i++)
            {
                if(strcmp(astd[i],ans[i])!=0)
                {
                    if(strcmp(na,ns)==0)match=1;
                    else match=2;
                    break;
                }
            }
        }
        else
        {
            if(strcmp(na,ns)==0)match=1;
            else match=2;
        }
        printf("Run #%d: %s
",T++,res[match]);
    }
    return 0;
}
/*
2
The answer is: 10
The answer is: 5
2
The answer is: 10
The answer is: 5

3
Input Set #1: YES
Input Set #2: NO
Input Set #3: NO
3
Input Set #0: YES
Input Set #1: NO
Input Set #2: NO
*/
View Code

 6、110306/10132 File Fragmentation (文件碎片)

枚举

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<ctype.h>
using namespace std;
int h[300];
char node[300][300];
int nxt[300];
int vis[300];
int mvis[300];
int cnt;
int match(char cmd[],int len)
{
    int i,j,k,tot=0;
    for(i=0;i<300;i++)mvis[i]=vis[i];
    char mat[300];
    for(i=1;i<len;i++)
    {
        for(j=h[i];j!=-1;j=nxt[j])
        {
            if(mvis[j]==1)continue;
            strcpy(mat,node[j]);
            mvis[j]=1;
            for(k=h[len-i];k!=-1;k=nxt[k])
            {
                if(mvis[k]==1)continue;
                strcat(mat,node[k]);
                mvis[k]=1;
                if(strcmp(mat,cmd)==0)
                {
                    tot++;
                    break;
                }
                else
                {
                    strcpy(mat,node[k]);
                    strcat(mat,node[j]);
                    if(strcmp(mat,cmd)==0)
                    {
                        tot++;
                        break;
                    }
                    else
                    {
                        return 0;
                    }
                }
            }
        }
    }
    //printf("tot=%d,cnt=%d
",tot,cnt);
    return tot==cnt/2-1;
}
void solve(int len)
{
    int i,tot;
    char cmd[300];
    for(i=1;i<len;i++)
    {
        if(h[i]!=-1)break;
    }
    int sta=h[i];
    vis[sta]=1;
    for(i=h[len-i];i!=-1;i=nxt[i])
    {
        strcpy(cmd,node[sta]);
        if(vis[i]==0)
        {
            vis[i]=1;
            strcat(cmd,node[i]);
            tot=match(cmd,len);
            if(!tot)
            {
                strcpy(cmd,node[i]);
                strcat(cmd,node[sta]);
                tot=match(cmd,len);
                if(match(cmd,len))
                {
                    printf("%s
",cmd);
                    return;
                }
                else
                {
                    vis[i]=0;
                }
            }
            else
            {
                printf("%s
",cmd);
                return;
            }
        }
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    getchar();getchar();
    while(T--)
    {
        memset(h,-1,sizeof(h));
        memset(nxt,-1,sizeof(nxt));
        memset(vis,0,sizeof(vis));
        cnt=0;
        int sum=0;
        while(gets(node[cnt])!=NULL)
        {
            if(node[cnt][0]=='')
            {
                break;
            }
            int len=strlen(node[cnt]);
            nxt[cnt]=h[len];
            h[len]=cnt;
            sum+=len;
            cnt++;
        }
        solve(sum*2/cnt);
        if(T)
            printf("
");
    }
}
/*
3

011
0111
01110
111
0111
10111

0
101110101
010
1110101
0101110
101
01011
10101
0101110
101
0101
110101
01011101
01
*/
View Code

 7、110307/10150 Doublets (Doublets 序列)

简化为图论模型,使用光搜,否则会超时

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<ctype.h>
#include<queue>
using namespace std;
char word[26000][20];
int h[26000];
int nxt[1000000];
int node[1000000];
int vis[26000];
int dis[26000];
int cnt=0;
int q[26000];
int pre[26000];
int route[26000];
int GetInd(char src[])
{
    for(int i=0;i<cnt;i++)
    {
        if(strcmp(src,word[i])==0)return i;
    }
    return -1;
}
int _strcmp(char src[],char dest[],int len)
{
    int tot=0;
    for(int i=0;i<len;i++)
        if(src[i]!=dest[i])tot++;
    return tot;
}
void solve(int s,int e)
{
    vis[s]=1;
    dis[s]=0;
    int i,front=0,rear=1,lev=1,end=rear,tot=0;
    q[front]=s;
    while(front<rear)
    {
        int x=q[front];
        front++;
        for(i=h[x];i!=-1;i=nxt[i])
        {
            int y=node[i];
            if(dis[y]==-1)
            {
                dis[y]=lev;
                pre[y]=x;
                if(y==e)
                {
                    int t=y;
                    tot=0;
                    while(1)
                    {
                        route[tot++]=t;
                        if(t==s)break;
                        t=pre[t];
                    }
                    for(tot=tot-1;tot>=0;tot--)printf("%s
",word[route[tot]]);
                    return;
                }
                q[rear]=y;
                rear++;
            }
        }
        if(front==end)
        {
            lev++;
            end=rear;
        }
    }
    printf("No solution.
");
}
int main()
{
    char src[20],dest[20];
    int T=0,i,tot=0;
    memset(h,-1,sizeof(h));
    memset(nxt,-1,sizeof(nxt));
    while(gets(word[cnt])!=NULL)
    {
        if(word[cnt][0]=='')
            break;
        int la=strlen(word[cnt]);
        for(i=0;i<cnt;i++)
        {
            int lb=strlen(word[i]);
            if(la!=lb)continue;
            if(_strcmp(word[i],word[cnt],la)==1)
            {
                node[tot]=cnt;
                nxt[tot]=h[i];
                h[i]=tot;
                tot++;
                node[tot]=i;
                nxt[tot]=h[cnt];
                h[cnt]=tot;
                tot++;
            }
        }
        cnt++;
    }
    while(scanf("%s",src)!=EOF)
    {
        scanf("%s",dest);
        if(T)printf("
");
        memset(vis,0,sizeof(vis));
        memset(dis,-1,sizeof(dis));
        memset(pre,-1,sizeof(pre));
        int sta=GetInd(src);
        int end=GetInd(dest);
        if(sta==-1||end==-1)
        {
            printf("No solution.
");
            continue;
        }
        solve(sta,end);
        T++;
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/varcom/p/4105980.html