题解 | #查找两个字符串a,b中的最长公共子串#
查找两个字符串a,b中的最长公共子串
https://www.nowcoder.com/practice/181a1a71c7574266ad07f9739f791506
#include<stdio.h> #include<string.h> int main() { char arr1[300]=""; char arr2[300]=""; char temp[300]=""; while(scanf("%s %s",arr1,arr2)!=EOF) { if(strlen(arr1)>strlen(arr2)) { strcpy(temp,arr1); strcpy(arr1,arr2); strcpy(arr2,temp); } int max=0; for(int i=0;i<strlen(arr1);i++) for(int j=0;j<strlen(arr2);j++) { int count=0; while(arr1[i+count]==arr2[j+count]) count++; if (count>max) { max=count; strcpy(temp,arr1+i); temp[max]='\0'; } } printf("%s\n",temp); } return 0; }