双指针(最大删除子串)Codeforces Round #579 (Div. 3)--Remove the Substring (hard version)
题目链接:https://codeforces.com/contest/1203/problem/D2
题意:
给你S串、T串,问你最长删除多长的子串使得S串里仍然有T的子序列。
思路:
想了好久,先正着跑一下S串,记录T串每一个字符最左边在哪里,再倒着跑一下,记录T串的每一个字符最右边在哪里。
最后跑一下答案:
1. 开头和结尾特判一下,但不是max( L[1]-1 , l1-R[l2] ) , 而是对两个max( L[1]-1 , l1-L[l2]-1 )、max( R[1]-1 , l1-R[l2]-1 )再取max。
2. 对于中间部分:R[i]-L[i-1]-1 。
1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0); 2 #include <cstdio>//sprintf islower isupper 3 #include <cstdlib>//malloc exit strcat itoa system("cls") 4 #include <iostream>//pair 5 #include <fstream> 6 #include <bitset> 7 //#include <map> 8 //#include<unordered_map> https://codeforces.com/contest/1203/problem/D2 9 #include <vector> 10 #include <stack> 11 #include <set> 12 #include <string.h>//strstr substr 13 #include <string> 14 #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9; 15 #include <cmath> 16 #include <deque> 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less 18 #include <vector>//emplace_back 19 //#include <math.h> 20 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor 21 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare) 22 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation 23 #define fo(a,b,c) for(register int a=b;a<=c;++a) 24 #define fr(a,b,c) for(register int a=b;a>=c;--a) 25 #define mem(a,b) memset(a,b,sizeof(a)) 26 #define pr printf 27 #define sc scanf 28 #define ls rt<<1 29 #define rs rt<<1|1 30 void swapp(int &a,int &b); 31 double fabss(double a); 32 int maxx(int a,int b); 33 int minn(int a,int b); 34 int Del_bit_1(int n); 35 int lowbit(int n); 36 int abss(int a); 37 //const long long INF=(1LL<<60); 38 const double E=2.718281828; 39 const double PI=acos(-1.0); 40 const int inf=(1<<29); 41 const double ESP=1e-9; 42 const int mod=(int)1e9+7; 43 const int N=(int)1e6+10; 44 45 int L[N],R[N]; 46 char s[N],t[N]; 47 48 int main() 49 { 50 // freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin); 51 int l1,l2; 52 s[0]=t[0]='$'; 53 // while(sc("%s%s",s+1,t+1)==2) 54 // { 55 sc("%s%s",s+1,t+1); 56 l1=strlen(s)-1; 57 l2=strlen(t)-1; 58 for(int i=1,pos=1;i<=l1&&pos<=l2;++i) 59 { 60 if(t[pos]==s[i]) 61 L[pos]=i,pos++; 62 } 63 for(int i=l1,pos=l2;i>=1&&pos>=1;--i) 64 { 65 if(t[pos]==s[i]) 66 R[pos]=i,pos--; 67 } 68 int ans=0; 69 ans=maxx(ans,maxx(L[1]-1,R[1]-1)); 70 ans=maxx(ans,maxx(l1-L[l2],l1-R[l2])); 71 for(int i=2;i<=l2;++i) 72 ans=maxx(ans,R[i]-L[i-1]-1); 73 pr("%d\n",ans); 74 // } 75 return 0; 76 } 77 78 /**************************************************************************************/ 79 80 int maxx(int a,int b) 81 { 82 return a>b?a:b; 83 } 84 85 void swapp(int &a,int &b) 86 { 87 a^=b^=a^=b; 88 } 89 90 int lowbit(int n) 91 { 92 return n&(-n); 93 } 94 95 int Del_bit_1(int n) 96 { 97 return n&(n-1); 98 } 99 100 int abss(int a) 101 { 102 return a>0?a:-a; 103 } 104 105 double fabss(double a) 106 { 107 return a>0?a:-a; 108 } 109 110 int minn(int a,int b) 111 { 112 return a<b?a:b; 113 }