Advertisement
juanjo12x

UVA_10100_Perfect_Match

Aug 7th, 2014
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <string>
  6. #include <cctype>
  7. #include <stack>
  8. #include <queue>
  9. #include <list>
  10. #include <vector>
  11. #include <map>
  12. #include <set>
  13. #include <sstream>
  14. #include <limits.h>
  15. #include <stdlib.h>
  16. #include <cmath>
  17. #define LL unsigned long long
  18. using namespace std;
  19. int lcs[1010][1010];
  20. int main() {
  21.      int cont=1;char s1[1010];char s2[1010];
  22.      while(gets(s1)){
  23.         gets(s2);
  24.         if(s1[0] == '\0' || s2[0] == '\0') {
  25.             printf("%2d. Blank!\n",cont++);continue;
  26.            
  27.         }
  28.      
  29.         string str1[1010], str2[1010];
  30.  
  31.                 int count1 = 0 , count2 = 0;
  32.                 bool isIn = false;
  33.                 for(int i = 0 ; i < strlen(s1) ; i++)
  34.                 {
  35.                         char c = s1[i];
  36.                         if(isalpha(c) || isdigit(c))
  37.                         {
  38.                                 if(!isIn)
  39.                                 {
  40.                                         count1++;
  41.                                         str1[count1] += c;
  42.                                         isIn = true;
  43.                                 }
  44.                                 else str1[count1] += c;
  45.                         }
  46.                         else isIn = false;
  47.                 }
  48.                  isIn = false;
  49.  
  50.                 for(int i = 0 ; i < strlen(s2) ; i++)
  51.                 {
  52.                         char c = s2[i];
  53.                         if(isalpha(c) || isdigit(c))
  54.                         {
  55.                                 if(!isIn)
  56.                                 {
  57.                                         count2++;
  58.                                         str2[count2] += c;
  59.                                         isIn = true;
  60.                                 }
  61.                                 else str2[count2] += c;
  62.                         }
  63.                         else isIn = false;
  64.                 }
  65.                 memset(lcs, 0, sizeof(lcs));
  66.                 for (int i=1;i<=count1;i++){
  67.                     for(int j=1;j<=count2;j++){
  68.                         if(str1[i]==str2[j]){
  69.                             lcs[i][j]=1+lcs[i-1][j-1];
  70.                         }else
  71.                         lcs[i][j]=max(lcs[i-1][j],lcs[i][j-1]);
  72.                     }
  73.                 }
  74.                 printf("%2d. Length of longest match: %d\n", cont++, lcs[count1][count2]);
  75.      }
  76.      return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement