Advertisement
Guest User

Untitled

a guest
Oct 12th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace SubstringCheck
  8. {
  9.     class Program
  10.     {
  11.         static string a = "";
  12.         static string b = "";
  13.         static char[] aa;
  14.         static char[] bb;
  15.         static bool result;
  16.         static int asterisks = 0;
  17.  
  18.         private static void Check(char[] A, char[] B)
  19.         {
  20.             int row = 0;
  21.             int doubleSigns = 0;
  22.             for (int i = 0; i < B.Length; i++)
  23.             {
  24.                 for (int j = 0; j < A.Length; j++)
  25.                 {
  26.                     bool flag = false;
  27.  
  28.                     //asterisk
  29.                     if (B[i] == '*')
  30.                     {
  31.                         if (i > 0)
  32.                         {
  33.                             if (B[i - 1] == 92)//    '\' sign -> treating asterisk as normal sign
  34.                             {
  35.                                 flag = true;
  36.                                 doubleSigns++;
  37.                             }
  38.                         }
  39.  
  40.                         if (flag == false)
  41.                         {
  42.                             if (row == B.Length - 1 - asterisks)
  43.                             {
  44.                                 result = true;
  45.                                 return;
  46.                             }
  47.                             asterisks++;
  48.                             for (int k = i + 1; k < B.Length; k++)//finding first non-aterisk char after asterisk
  49.                             {
  50.                                 if (B[k] != '*')
  51.                                 {
  52.                                     i = k;
  53.                                     break;
  54.                                 }
  55.                                 else
  56.                                 {
  57.                                     asterisks++;
  58.                                 }
  59.                             }
  60.                             if (row == B.Length - asterisks) // end of line B check
  61.                             {
  62.                                 result = true;
  63.                                 return;
  64.                             }
  65.                             for (int l = j; l < A.Length; l++)
  66.                             {
  67.                                 if (B[i] == A[l])
  68.                                 {
  69.                                     j = l;
  70.                                     break;
  71.                                 }
  72.                             }
  73.                         }
  74.                     }
  75.                     //treating '\' and next asterisk as one char
  76.                     if (B[i] == 92)
  77.                     {
  78.                         if(B.Length > i + 1)
  79.                         {
  80.                             if(B[i+1] == '*')
  81.                             {
  82.                                 i++;
  83.                                 j--;
  84.                                 continue;
  85.                             }
  86.                         }
  87.                     }
  88.  
  89.                     //
  90.                     if (B[i] == A[j] || (B[i] == '*' && A[j] == '*' && flag == true))
  91.                     {
  92.                         row++;
  93.                         if (i > 0 && row < 1)
  94.                         {
  95.                             return;
  96.                         }
  97.                         if (i <= A.Length - 2)
  98.                         {
  99.                             i++;
  100.                         }
  101.                         if (row == B.Length - asterisks - doubleSigns)
  102.                         {
  103.                             result = true;
  104.                             return;
  105.                         }
  106.                     }
  107.                     else
  108.                     {
  109.                         row = 0;
  110.                         if(B.Length == i + 1)//end of line B check
  111.                         {
  112.                             return;
  113.                         }
  114.                     }
  115.                 }
  116.             }
  117.         }
  118.  
  119.         static void Main(string[] args)
  120.         {
  121.             result = false;
  122.             Console.WriteLine("Podaj pierwszy ciag:");
  123.             a = Console.ReadLine();
  124.             aa = a.ToCharArray();
  125.             Console.WriteLine("Podaj drugi ciag:");
  126.             b = Console.ReadLine();
  127.             bb = b.ToCharArray();
  128.             if (a.Length >= b.Length)
  129.             {
  130.                 Check(aa, bb);
  131.             }
  132.             Console.WriteLine("---");
  133.             if (result)
  134.             {
  135.                 Console.WriteLine("Drugi ciag jest podciagiem pierwszego");
  136.             }
  137.             else
  138.             {
  139.                 Console.WriteLine("Drugi ciag NIE jest podciagiem pierwszego");
  140.             }
  141.             Console.Read();//
  142.         }
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement