2017年6月11日 星期日

(11)瘋狂程設->CPE顆星廣場

一個十字交錯字由兩個單字組成,第一個單字以水平排列,第二個單字以垂直排列,於交錯的位置共用同一個字母,其共用字母必須盡可能接近水平文字的開頭,在此情況下亦盡可能接近垂直文字的開頭。例如"DEFER""PREFECT"的共同字母為彼此的第一個"E",而"PREFECT""DEFER"的共同字母為"R"。雙十字交錯字由四個單字組成,兩個水平單字必須在同一列。

本題給定每組四個單字(水平1, 垂直1, 水平2, 垂直2),請你輸出每組雙十字交錯字。

INPUT
會有多列輸入資料,每列有四個單字,兩兩一組,每個單字包含1~10個大寫字母,每個單字之間最少以一個空白字元隔開。以一列僅含一個"#"字元表示測試資料結束。

OUTPUT
請對每組測試資料輸出雙十字交錯字,水平文字間以三個空白字元隔開,如果無法組成這兩個十字交錯字,請輸出"Unable to make two crosses"。請每組測試資料間輸出一列空行。

SAMPLE INPUT
MATCHES CHEESECAKE PICNIC EXCUSES
PEANUT BANANA VACUUM  GREEDY
#
SAMPLE OUTPUT
 C
 H
 E
 E
 S
 E          E
 C          X
MATCHES   PICNIC
 K          U
 E          S
            E
            S

Unable to make two crosses
第一部分
static void Main(string[] args)
        {
            while (true)
            {
                string Input = Console.ReadLine();
                if (Input == "#") return;
                string[] sentence = Input.Split(' ');
                int h0 = 0, h1 = -1;
                string blank1 = null;

                List<char> c1 = new List<char>();
                c1.AddRange(sentence[1].ToCharArray());
                foreach (char c0 in sentence[0])
                {
                    if (c1.IndexOf(c0) != -1)
                    {
                        h1 = c1.IndexOf(c0);
                        break;
                    }
                    h0++;
                }
                for (int i = 0; i < h0; i++)
                    blank1 = blank1 + " ";
                for (int i = 0; i < c1.Count; i++)
                {
                    if (i == h1) Console.WriteLine(sentence[0]);
                    else Console.WriteLine(blank1 + c1[i]);
                }
            }
        }
第二部分

static void Main(string[] args)
        {
            while (true)
            {
                string Input = Console.ReadLine();
                if (Input == "#") return;
                string[] sentence = Input.Split(' ');
                string blank1 = null, blank2 = null, anwser1 = null;
                string[] anwser2 = new string[20];
                int h0 = 0, h1 = 0, h2 = 0, h3 = 0, h4 = 0, h5 = 0;

                List<char> c1 = new List<char>();
                c1.AddRange(sentence[1].ToCharArray());
                List<char> c3 = new List<char>();
                c3.AddRange(sentence[3].ToCharArray());
                foreach (char c0 in sentence[0])
                {
                    h1 = c1.IndexOf(c0);
                    if (c1.IndexOf(c0) == -1) h0++;
                    else break;
                }
                foreach (var c2 in sentence[2])
                {
                    h3 = c3.IndexOf(c2);
                    if (c3.IndexOf(c2) == -1) h2++;
                    else break;
                }
                if (h1 == -1 || h3 == -1)               
                    Console.WriteLine("Unable to make two crosses");               
                else
                {
                    for (int i = 0; i < h0; i++)
                        blank1 = blank1 + " ";
                    for (int i = 0; i < h2; i++)
                        blank2 = blank2 + " ";
                    for (int i = 0; i < c3.Count; i++)
                    {
                        if (i == h3) anwser2[i] = "\t\t" + sentence[2] + "\n";
                        else anwser2[i] = "\t\t" + blank2 + c3[i] + "\n";
                    }
                    for (int i = 0; i < c1.Count; i++)
                    {
                        if (i < h1 - h3)
                        {
                            if (i == h1) anwser1 += sentence[0] + "\n";
                            else anwser1 += blank1 + c1[i] + "\n";
                            h4++;
                        }
                        else
                        {
                            if (i == h1) anwser1 += sentence[0] + anwser2[i - h4];
                            else anwser1 += blank1 + c1[i] + anwser2[i - h4];
                            h5++;
                        }
                    }
                    for (int i = c1.Count + 1; i <= c3.Count + h4; i++)
                    {
                        anwser1 = anwser1 + anwser2[h5];
                        h5++;
                    }
                    Console.WriteLine(anwser1);
                }
                Console.WriteLine();               
            }        
        }

11.Common Permutation

給定兩個小寫的字串ab,請印出皆出現在兩字串中的字母,出現的字母由a~z的順序印出,若同字母出現不只一次,請重複印出但不能超過任一字串中出現的次數。
(原文直譯:給定兩個小寫的字串ab,請印出最長的小寫字串x,使得x經重新排列組合後為a的子字串(subsequence of a),且x經重新排列組合後亦為b的子字串)
Input
輸入會有好幾列字串,每兩列一組(1 2一組,3 4一組,以此類推),每組的第一個字串為a,第二個為b,每個字串一列,其長度最多有1000個小寫字元。

Output
輸出本題所要求的x,若有多組符合的x,請印出字母順序由小到大排列的那一個。

Sample Input:
pretty
women
walking
down
the
street

Sample Output:
e
nw
et


static void Main(string[] args)
        {
            List<string> collect = new List<string>();
            while (true)
            {
                string Input = Console.ReadLine();
                if (Input == "") break;
                collect.Add(Input);
            }
            for (int i = 0; i < collect.Count; i+=2)
            {
                for (int j = 0; j < 26; j++)
                {
                    char letter = Convert.ToChar(j + 97);
                    bool b1 = false, b2 = false;
                    foreach (char item in collect[i])
                        if (item == letter) b1 = true;
                    foreach (char item in collect[i+1])
                        if (item == letter) b2 = true;
                    if (b1==true && b2 == true)
                        Console.Write(letter.ToString());
                }
                Console.WriteLine();
            }
        }

WinFormTb02

https://drive.google.com/drive/u/0/folders/1UwS9FZ3ELCOK6SAwirHrkxq3z_RSbxJt https://www.youtube.com/watch?v=k7IkIeww_U0&list=PLumjEWemD...