Test instructions: Give two fruit name, require their LCS part only output 1 times, other normal output, but must keep the original order!
Idea: To find the LCS is normal, but the output trouble, to first seek LCS, and then mark the two strings of all the LCS characters, in the case of LCS characters, the first to lose string 1, then the string 2, and then lose the character, in order to ensure that each LCS character output before the character in both strings before the characters are all output.
1 //#pragma COMMENT (linker, "/stack:102400000,102400000")2#include <iostream>3#include <stdio.h>4#include <string.h>5#include <vector>6#include <stack>7#include <algorithm>8#include <map>9#include <bits/stdc++.h>Ten #defineLL Long Long One #definePII pair<int,int> A #defineINF 0x7f7f7f7f - using namespacestd; - Const intn= -+5; the CharS1[n], s2[n]; - - intMark[n][n]; - intCnt[n][n]; + - intPos1[n]; + intPos2[n]; A at voidLCS () - { -memset (Mark,0,sizeof(Mark)); -memset (CNT,0,sizeof(CNT)); - intLen1=strlen (s1+1); - intLen2=strlen (s2+1); in - for(intI=1; i<=len1; i++) to { + for(intj=1; j<=len2; J + +) - { the if(s1[i]==S2[j]) * { $cnt[i][j]=cnt[i-1][j-1]+1;Panax Notoginsengmark[i][j]=3;//Slant of - } the Else if(cnt[i-1][j]>=cnt[i][j-1]) + { Acnt[i][j]=cnt[i-1][j]; themark[i][j]=2;//Top + } - Else $ { $cnt[i][j]=cnt[i][j-1]; -mark[i][j]=1;//left - } the } - }Wuyi intT1=len1, t2=Len2; thememset (POS1,0,sizeof(POS1)); -memset (Pos2,0,sizeof(Pos2)); Wu while(t1>0&& t2>0) - { About intPre=Mark[t1][t2]; $ - if(pre==3) - { -pos1[t1]=1; Apos2[t2]=1; +T1--, t2--; the } - Else if(pre==2) t1--; $ Elset2--; the } the intI=1, j=1; the the while(1) - { in while(I<=len1 &&!)Pos1[i]) theprintf"%c", s1[i++]); thei++; About while(J<=len2 &&!)Pos2[j]) theprintf"%c", s2[j++]); the if(J<=LEN2) printf ("%c", S2[j]); theJ + +; + if(I>LEN1&&J>LEN2)return ; - } the }Bayi the intMain () the { -Freopen ("Input.txt","R", stdin); - while(~SCANF ("%s%s", s1+1, s2+1)) the { the LCS (); theprintf"\ n"); the } - return 0; the}
AC Code
HDU 1503 Advanced Fruits (LCS, longest common substring, variant)