//还是too young,因为没初始化len_name, len_str wa了一发
1 #include "bits\stdc++.h" 2 using namespace std; 3 char name[1010], str[1000010]; 4 5 int main() 6 { 7 scanf("%s %s", name, str); 8 int len_name = strlen(name), len_str = strlen(str); 9 int i, j; 10 int pos_1, pos_2; 11 pos_1 = len_str; 12 pos_2 = -1; 13 for(i = j = 0; i < len_str; ++i) { 14 if(str[i] == name[j]) { 15 ++j; 16 if(j == len_name) { 17 pos_1 = i; 18 break; 19 } 20 } 21 } 22 for(i = len_str - 1, j = len_name - 1; i >= 0; --i) { 23 if(str[i] == name[j]) { 24 --j; 25 if(j == -1) { 26 pos_2 = i; 27 break; 28 } 29 } 30 } 31 printf("%d\n", max(0, pos_2 - pos_1)); 32 }
Codeforces 523C - Name Quest 手速题
原文:http://www.cnblogs.com/AC-Phoenix/p/4374810.html