CMB9. 字符串是否由子串拼接
描述
输入描述
非空字符串输出描述
如果字符串满足上述条件,则输出最长的满足条件的的子串;如果不满足条件,则输出false。示例1
输入:
abcabc
输出:
abc
C 解法, 执行用时: 1ms, 内存消耗: 368KB, 提交时间: 2019-05-26
#include<stdio.h> #include<math.h> #include<stdlib.h> #include<string.h> int main() { char s[100000]; char s1[100000]; int i,j; int flag=0; int len1; scanf("%s",s); int len=strlen(s); int k; for(i=2;i<=len;i++) { if(len%i==0) { len1=len/i;//printf("________%d\n",len1); for(j=0;j<len1;j++) { k=j+len1; while(k<len) { if(s[j]==s[k]) k+=len1; else { flag=2; break; } } if(flag==2) { break; } } //printf("________%d\n",k); if(j==len1||k>=len) { strncpy(s1,s,len1); s1[len1]='\0'; printf("%s\n",s1); return 0; } } } if(i==len+1) { printf("false\n"); return 0; } return 0; }
C 解法, 执行用时: 2ms, 内存消耗: 232KB, 提交时间: 2019-07-23
#include <stdio.h> #include <string.h> int main() { char a[10000]; scanf("%s",a); char b[10000]; int len = strlen(a); int len1 ; int i , k , j; int flag = 0; for (i = 2; i <= len; i++) { flag=0; if (len%i == 0) { len1 = len / i; for ( j = 0; j < len1; j++) { k = j + len1; while (k < len) { if (a[j] == a[k]) k += len1; else { flag = 2; break; } } if (flag == 2) { break; } } if (j == len1 || k >= len) { strncpy(b, a, len1); b[len1] = '\0'; printf("%s", b); return 0; } } } if (i == len + 1) { printf("false"); return 0; } }
C 解法, 执行用时: 2ms, 内存消耗: 232KB, 提交时间: 2018-11-15
#include <stdio.h> #include <string.h> int check(char *str, int len, int shortlen){ int n = len/shortlen; int i, j; for(i = 0; i < shortlen; i ++){ for(j = 1; j < n; j ++){ if(str[j] != str[i*shortlen + j]) return -1; } } return 0; } int main(void){ char buf[512]; scanf("%s", buf); if( buf[0] == '\0' ) goto NO; int times = 0; int len = strlen(buf); int i, j; for(i = 0; i < len; i ++) if( buf[i] == buf[0] ) times ++; if( times == 1 || 0 != len % times ) goto NO; if( 0 == times % 2 && 0 == check(buf, len, i) ){ i = 2; goto YES; } for(i = 3; i <= times; ){ if( 0 == check(buf, len, i) ) goto YES; i ++; for(j = 2; j <= i/2 && i <= times; j += 2 ){ if( 0 == i % j ){ j = 2; i ++; } } } YES: buf[len/i] = '\0'; printf("%s", buf); return 0; NO: printf("false"); return 0; }
C 解法, 执行用时: 2ms, 内存消耗: 328KB, 提交时间: 2019-04-07
#include <stdio.h> #include <string.h> int check(char *str, int len, int shortlen){ int n = len/shortlen; int i, j; for(i = 0; i < shortlen; i ++){ for(j = 1; j < n; j ++){ if(str[j] != str[i*shortlen + j]) return -1; } } return 0; } int main(void){ char buf[512]; scanf("%s", buf); if( buf[0] == '\0' ) goto NO; int times = 0; int len = strlen(buf); int i, j; for(i = 0; i < len; i ++) if( buf[i] == buf[0] ) times ++; if( times == 1 || 0 != len % times ) goto NO; if( 0 == times % 2 && 0 == check(buf, len, i) ){ i = 2; goto YES; } for(i = 3; i <= times; ){ if( 0 == check(buf, len, i) ) goto YES; i ++; for(j = 2; j <= i/2 && i <= times; j += 2 ){ if( 0 == i % j ){ j = 2; i ++; } } } YES: buf[len/i] = '\0'; printf("%s", buf); return 0; NO: printf("false"); return 0; }
C 解法, 执行用时: 2ms, 内存消耗: 348KB, 提交时间: 2018-09-08
# include <stdio.h> # include <stdlib.h> # include <string.h> int main(){ char str[100]; char *p=str; int n,i,j,m,z,c; gets(str); n=strlen(str); for(i=0;i<n;i++){ for(j=1;j<n;j++){ if(*(p+i)==*(p+j)) for(m=0;m<n;m=m+j){ for(z=0;z<j;z++) if(*(p+z)==*(p+m+z)) c=j; else c=0; } else continue; break; } break; } i=0; if(c) while(i<j){ printf("%c",*(p+i)); i++; } else printf("false"); return 0; }