HDU 1503 Advanced Fruits (LCS, longest common substring, variant)

Source: Internet
Author: User

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&LT;=LEN2) printf ("%c", S2[j]); theJ + +; +         if(I&GT;LEN1&AMP;&AMP;J&GT;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.