编程: 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