这个题 还是比较简单的; 把案例 想出来是怎么来的;差不多就出来了; 只要从前往后扫,遇到一个子序列就统计一次,更新起点;
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 ",res); 28 } 29 return 0; 30 }