FZU Problem 2183 简单题(字符串处理)

2015-07-20 17:09:02 ? 作者: ? 浏览: 2

FZU Problem 2183 简单题

Problem Description

现在有一些被简单压缩的字符串,例如:a[120]代表120个a。对于字符串acb[3]d[5]e相对于acbbbddddde

现在给你两个字符串cString, nString.一个是被压缩过的字符串,另一个没有被压缩。

求nString是否为cString的子串,如果是输出True,否则输出False.cString的长度clen的范围是0

Sample Input

acb[3]d[5]e
bd

Sample Output

True

题目大意:中文题目。

解题思路:将后一个没有压缩的字符串进行压缩,然后与第一个压缩过的字符串进行比较,注意比较时要注意第一个和最后一个:两者的字符要相同,并且前一个的字符出现次数要大于等于后一个。

#include 
   
     #include 
    
      #include 
     
       #include 
      
        #include 
       
         using namespace std; #define N 1005 typedef long long ll; struct C{ int cnt; char ch; }; C A[N], B[N]; int main() { while (1) { for (int i = 0; i < N; i++) { A[i].cnt = 1; } char temp, t = ' '; int cnt1 = -1, cnt2 = -1; while (scanf("%c", &temp), temp != '\n') { if (t == temp) A[cnt1].cnt++; else if (temp != '[' && t != temp) { cnt1++; } if (temp >= 'a' && temp <= 'z') { A[cnt1].ch = temp; t = temp; } if (temp == '[') { int num; scanf("%d%*c", &num); A[cnt1].cnt = num; } } t = ' '; while (scanf("%c", &temp), temp != '\n') { if (temp >= 'a' && temp <= 'z' && temp != t) { B[++cnt2].ch = temp; B[cnt2].cnt = 1; t = temp; } else if (temp == t) { B[cnt2].cnt++; } } int flag = 0; for (int i = 0; i <= cnt1; i++) { if (A[i].ch == B[0].ch && A[i].cnt >= B[0].cnt) { int flag2 = 1; for (int j = 1; j < cnt2; j++) { if (A[i + j].ch != B[j].ch || A[i + j].cnt != B[j].cnt) { flag2 = 0; break; } } if (flag2) { if (A[i + cnt2].ch == B[cnt2].ch && A[i + cnt2].cnt >= B[cnt2].cnt) { flag = 1; break; } } } } if (cnt1 == -1 && cnt2 == -1) break; if (flag) printf("True\n"); else printf("False\n"); } return 0; }
       
      
     
    
   
-->

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: