编程: p10252

编程: p10252

题目

Given two strings of lowercase letters, a and b, print the longest string x of lowercase letters such that there is a permutation of x that is a subsequence of a and there is a permutation of x that is a subsequence of b.

Input

Input le contains several lines of input.Consecutive two lines make a set of input. That means in the input le line 1 and 2 is a set of input, line 3 and 4 is a set of input and so on. The rst line of a pair contains a and the second contains b. Each string is on a separate line and consists of at most 1000 lowercase letters.

Output

For each set of input, output a line containing x. If several x satisfy the criteria above, choose the rst one in alphabetical order.

Sample Input

pretty
women
walking
down
the
street

Sample Output

e
nw
et

解题方法1:

#include<stdio.h>
#include<memory.h>
void findSameChar(char* str1,char* str2,char same[]) ;

int main()
{
    char str1[1000] ;
    char str2[1000] ;
    char same[128] ;
    char i ;

    //Given two strings of lowercase letters
    //输入两个小写字符串,所以每个字符串不能有空格
    FILE* fileRead = fopen("C:\\Users\\Administrator\\Desktop\\test10252.txt","r");
    if(NULL==fileRead)
        return 0;

    while(fscanf(fileRead,"%s",str1)!=EOF && fscanf(fileRead,"%s",str2)!=EOF)
    {
        memset(same,0,sizeof(same)) ;
        findSameChar(str1,str2,same) ;
        //打印
        for(i='a' ;i<='z' ;++i)
        {   
            if(same[i]!= 0 )
                printf("%c",i) ;
        }
        printf("\n") ;  
    }

    fclose(fileRead) ;
    return 0 ;
}


void findSameChar(char* str1,char* str2,char same[])
{
    char* p1 = str1 ;
    char* p2 = str2 ;
    if(*p1!='\0')
    {
        while(*p2!='\0')
        {
            if(*p1==*p2)
            {
                same[*p1]++ ;
                break ;
            }
            p2++ ;
        }
        findSameChar(++p1 ,str2,same) ;
    }   
}

解题方法2:

#include<stdio.h>
#include<memory.h>

int main()
{
    char str1[1000] ;
    char str2[1000] ;
    char *p1 ,*p2 ;
    int same[26] ;//26个小写字母
    char i ;
    //读文件
    FILE* fileRead = fopen("C:\\Users\\Administrator\\Desktop\\test10252.txt","r");
    if(NULL==fileRead)
        return 0;

    while(fscanf(fileRead,"%s",str1)!=EOF && fscanf(fileRead,"%s",str2)!=EOF)
    {
        memset(same,0,sizeof(same)) ;
        p1 = str1 ;
        p2 = str2 ;
        //str1
        while(*p1!='\0')
        {
            if(same[*p1-'a']==0)
                same[*p1-'a']=1;
            p1++ ;
        }
        //str2
        while(*p2!='\0')
        {
            if(same[*p2-'a']==1)
                same[*p2-'a']=2;
            p2++ ;
        }

        //打印
        for(i='a' ;i<='z' ;++i)
        {   
            if(same[i-'a']== 2 )
                printf("%c",i) ;
        }
        printf("\n") ;

    }
    //关闭文件
    fclose(fileRead) ;
    return 0 ;
}

解题方法3:

#include<stdio.h>
#include<memory.h>

int main()
{
    char str1[1000] ;
    char str2[1000] ;
    char *p1 ,*p2 ;
    int same[26] ;//26个小写字母
    char i ;
    //读文件
    FILE* fileRead = fopen("C:\\Users\\Administrator\\Desktop\\test10252.txt","r");
    if(NULL==fileRead)
        return 0;

    while(fscanf(fileRead,"%s",str1)!=EOF && fscanf(fileRead,"%s",str2)!=EOF)
    {
        memset(same,0,sizeof(same)) ;
        p1 = str1 ;
        p2 = str2 ;

        //str1和str2同时进行
        while(*p1!='\0'|| *p2!='\0' )
        {
            if( *p1!='\0'&& (same[*p1-'a']==0 || same[*p1-'a']==1000 ) )
                same[*p1-'a']++;
            if( *p2!='\0'&& (same[*p2-'a']==0 || same[*p2-'a']==1 )    )
                same[*p2-'a']+=1000;
            if(*p1!='\0')
                p1++ ;
            if(*p2!='\0')
                p2++ ;
        }

        //打印
        for(i='a' ;i<='z' ;++i)
        {   
            if(same[i-'a']== 1001 )
                printf("%c",i) ;
        }
        printf("\n") ;

    }
    //关闭文件
    fclose(fileRead) ;
    return 0 ;
}

其中test10225.txt

pretty
women
walking
down
the
street

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值