zoukankan      html  css  js  c++  java
  • 哈希UVALive 6326 Contest Hall Preparation

     
     
     
     
     
     
     

     
     
     
     
     
     
    Encrypting passwords is one of the most important problems nowadays, and you trust only the encryp-
    tion algorithms which you invented, and you have just made a new encryption algorithm.
    Given a password which consists of only lower case English letters, your algorithm encrypts this
    password using the following 3 steps (in this given order):
    1.
    Swap two different characters of the given password (you can do this step zero or more times).
    2.
    Append zero or more lower case English letters at the beginning of the output of step one.
    3.
    Append zero or more lower case English letters to the end of the output of step two.
    And the encrypted password is the output of step three.
    You have just nished implementing the above algorithm and applied it on many passwords. Now
    you want to make sure that there are no bugs in your implementation, so you decided to write another
    program which validates the output of the encryption program. Given the encrypted password and the
    original password, your job is to check whether the encrypted password may be the result of applying
    your algorithm on the original password or not.
    Input
    Your program will be tested on one or more test cases. The rst line of the input will be a single
    integer
    T
    , the number of test cases (1
    
    T
    
    100). Followed by the test cases, each test case is on two
    lines. The rst line of each test case contains the encrypted password. The second line of each test case
    contains the original password. Both the encrypted password and the original password are at least 1
    and at most 100,000 lower case English letters (from `
    a
    ' to `
    z
    '), and the length of the original password
    is less than or equal the length of the encrypted password.
    Output
    For each test case, print on a single line one word, `
    YES
    ' (without the quotes) if applying the algorithm on
    the original password may generate the encrypted password, otherwise print `
    NO
    ' (without the quotes).
    SampleOutput
    3
    abcdef
    ecd
    cde
    ecd
    abcdef
    fed
    SampleOutput
    YES
    YES
    NO//有坑
     
     
     
    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int maxn=1000;
    int hash[maxn];
    int cur[maxn];
    char s[100005],t[100005];
    int h[100005];
    int main()
    {
        int T;
        scanf("%d",&T);
        getchar();
        while(T--)
        {
            memset(h,0,sizeof(h));
            memset(hash,0,sizeof(hash));
            memset(cur,0,sizeof(cur));
            memset(t,0,sizeof(t));
            memset(s,0,sizeof(s));
            scanf("%s",t);
            getchar();
            scanf("%s",s);
            getchar();
            int len1=strlen(s);
            int len2=strlen(t);
    
            for(int i=0; i<len1; i++)
            {
                int temp=s[i];
                hash[temp]++;
            }
    
            int ans=0;
            int cnt=0;
            bool flag=false;
            if(len1==len2)
            {
                for(int i=0; i<len2; i++)
                {
                    int d=t[i];
                    h[i]=d;
                    if(cur[d]<hash[d])
                        cnt++;
                    cur[d]++;
                    if(cnt==len1){
                        flag=true;
                          break;
                        }
                }
            }
    
    
            else
            {
                for(int i=0; i<len2; i++)
                {
    
                    int d=t[i];
                    h[i]=d;
                    if(cur[d]<hash[d])
                        cnt++;
                    cur[d]++;
    
                    if(i>=len1)
                    {
                        d=h[i-len1];
                        if(cur[d]<=hash[d])
                            cnt--;
                        cur[d]--;
    
                    }
                    if(cnt==len1){
                        flag=true;
                          break;
                        }
    
                }
            }
            if(flag)
                printf("YES
    ");
            else
                printf("NO
    ");
        }
        return 0;
    }
     
     
     
     
     
  • 相关阅读:
    通过xshell在本地win主机和远程linux主机传输文件
    CentOS7.4搭建ftp服务
    CentOS7.4中配置jdk环境
    spring AOP学习笔记
    java代码连接oracle数据库的方法
    通过xshell上传和下载文件
    java设计模式简述
    本地项目文件通过git提交到GitHub上
    centos7中oracle数据库安装和卸载
    centos7远程服务器中redis的安装与java连接
  • 原文地址:https://www.cnblogs.com/13224ACMer/p/4692962.html
Copyright © 2011-2022 走看看