ACM PKU 1226 Substrings 较复杂字符串操作(但不繁琐)

ACM PKU 1226 Substrings 较复杂字符串操作(但不繁琐)

http://acm.pku.edu.cn/JudgeOnline/problem?id=1226

注意几点: 1.对字符串复制使用strcpy和strncpy  2.strstr 查找子串; 3.strrev对字符串反序  4.strncpy后要在末尾加上'\0'   5.二维数组放在全局位置
 
#include " stdio.h "
#include
" string.h "

int  n;
char  str[ 100 ][ 100 ];
int  searchMaxSubString( char   *  source)
{
    
int subStrLen=strlen(source),sourceStrLen=strlen(source);
    
int i,j;
    
bool foundMaxSubStr;
    
char subStr[101],revSubStr[101];

    
while(subStrLen>0)
    
{
        
for(i=0;i<=sourceStrLen-subStrLen;i++)
        
{
            strncpy(subStr,source
+i,subStrLen);
            strncpy(revSubStr,source
+i,subStrLen);
            subStr[subStrLen]
=revSubStr[subStrLen]='\0';
            strrev(revSubStr);
            
            foundMaxSubStr
=true;
            
for(j=0;j<n;j++)
                
if(strstr(str[j],subStr)==NULL&&strstr(str[j],revSubStr)==NULL){foundMaxSubStr=false;break;}
            
if(foundMaxSubStr) return subStrLen;

        }

        subStrLen
--;
    }

    
return 0;
}


void  main()
{
    
int i,nCase,minStrLen,subStrLen;
    
char minStr[101];
    scanf(
"%d",&nCase);
    
while(nCase--)
    
{

    scanf(
"%d",&n);
    minStrLen
=101;
    
for(i=0;i<n;i++)
    
{
        scanf(
"%s",str[i]);
        
if(strlen(str[i])<minStrLen){strcpy(minStr,str[i]);minStrLen=strlen(minStr);}

    }

    printf(
"%d\n",searchMaxSubString(minStr));
    }

    
}

你可能感兴趣的:(ACM PKU 1226 Substrings 较复杂字符串操作(但不繁琐))