SGU 506 Subsequences Of Substrings

这个题 还是比较简单的;  把案例 想出来是怎么来的;差不多就出来了; 只要从前往后扫,遇到一个子序列就统计一次,更新起点;

复制代码
 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <cmath>
 6 using namespace std;
 7 
 8 char str[112345],cha[112];
 9 int main( )
10 {
11     while( scanf("%s%s",str+1,cha+1) != EOF )
12     {
13         int len1 = strlen(str+1); int len2 = strlen(cha+1);
14         int pre = 0; int now = 0; int k = 1; long long res = 0; int i = 1;
15         while( i <= len1 )
16         {
17             if( str[i] == cha[k] )
18             {
19                 if( k == 1 )now = i;
20                 if( k == len2 )
21                 {
22                     res += (long long)(now-pre)*(long long)(len1-i+1);
23                     k = 1; pre = now; i = now+1; now = 0;
24                 }else { i++; k++; }
25             }else i++;
26         }
27         printf("%lld\n",res);
28     }
29     return 0;
30 }
复制代码

 

posted on   浪舟  阅读(361)  评论(0编辑  收藏  举报

导航

< 2025年1月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8

统计

点击右上角即可分享
微信分享提示