FZU三月月赛A——字符串处理——简单题
Problem Description
现在有一些被简单压缩的字符串,例如:a[120]代表120个a。对于字符串acb[3]d[5]e相对于acbbbddddde
现在给你两个字符串cString, nString.一个是被压缩过的字符串,另一个没有被压缩。
求nString是否为cString的子串,如果是输出True,否则输出False.cString的长度clen的范围是0<clen<1000, nString的长度的nlen的范围是0<nlen<1000;cString只包含小写26个字母,[],数字(大于0小于10^9)。nString只包含小写26个字母。
Sample Input
acb[3]d[5]e bd
Sample Output
True
Source
FOJ有奖月赛-2015年03月大意:就这样~考验代码能力
#include<cstring> #include<cstdio> using namespace std; const int MAX = 1100; char s1[MAX],s2[MAX]; char x1[MAX],x2[MAX]; int num1[MAX],num2[MAX]; int number(int i,int j){ int sum = 0; for(int k = j ; k >= i; k--) sum = sum*10 + s1[k] - '0'; return sum; } int main() { while(~scanf("%s%s",s1,s2)){ memset(num1,0,sizeof(num1)); memset(num2,0,sizeof(num2)); int len1 = strlen(s1); int k = 0; for(int i = 0 ; i < len1 ;i++) num1[i] = 1; for(int i = 0 ; i < len1;i++){ if(i == 0){ x1[k] = s1[i]; if(s1[i+1] == '['){ int j; for( j = i + 2;; j++) if(s1[j] ==']' ) break; int num = number(i+2,j-1); num1[k] = num; } } else { if(s1[i] >= 'a' && s1[i] <= 'z' &&s1[i] != x1[k]){ k++; x1[k] = s1[i]; if(s1[i+1] == '['){ int j; for( j = i + 2;;j++) if(s1[j] == ']') break; int num = number(i+2,j-1); num1[k] = num; } } else num1[k]++; } } // int l1 = k; // for(int i = 0; i <= l1; i++) // printf("%c",x1[i]);*/ // for(int i = 0; i <= l1;i++) // printf("%d ",num1[i]); k = 0; int len2 = strlen(s2); for(int i = 0 ; i < len2; i++) num2[i] = 1; for(int i = 0 ; i <len2;i++){ if(i == 0){ x2[k] = s2[i]; } else{ if(x2[k] == s2[i]) num2[k]++; else { k++; x2[k] = s2[i]; } } } int l2 = k; /* printf("%d",l2); for(int i = 0 ; i <= l2 ;i++) printf("%d",num2[i]);*/ int flag = 0;//judge k = 0; int i; if( l2 == 0 ){ int i; for( i = 0 ; i <= l1;i++) if(x1[i] == x2[0]) break; if(i <= l1) { puts("True"); continue; } } for( i = 0; i <= l1; i++){ if(x1[i] == x2[0] && num1[i] >= num2[0] && x1[i+l2] == x2[l2] && num1[i+l2] >= num2[l2]){ for(int j = 1; j < l2 ; j++){ if(x1[j+i] == x2[j] && num1[j+i] == num2[j]) flag++; } if(flag+2 == l2+1) break; } } //printf("%d",l2); // printf("%c %c",x1[0],x2[0]); if(i <= l1) puts("True"); else puts("False"); } return 0; }