1. 程式人生 > >fzuacm 2017 新生寒假訓練 7

fzuacm 2017 新生寒假訓練 7

A - a

While Mahmoud and Ehab were practicing for IOI, they found a problem which name was Longest common subsequence. They solved it, and then Ehab challenged Mahmoud with another problem.

Given two strings a and b, find the length of their longest uncommon subsequence, which is the longest string that is a subsequence of one of them and not a subsequence of the other.

A subsequence of some string is a sequence of characters that appears in the same order in the string, The appearances don’t have to be consecutive, for example, strings “ac”, “bc”, “abc” and “a” are subsequences of string “abc” while strings “abbc” and “acb” are not. The empty string is a subsequence of any string. Any string is a subsequence of itself.

Input:

The first line contains string a, and the second line — string b. Both of these strings are non-empty and consist of lowercase letters of English alphabet. The length of each string is not bigger than 105 characters.

Output:

If there’s no uncommon subsequence, print “-1”. Otherwise print the length of the longest uncommon subsequence of a

and b.

Example:

Input:

abcd
defgh

Output:

5

Input:

a
a

Output:

-1

Note:

In the first example: you can choose “defgh” from string b as it is the longest subsequence of string b that doesn’t appear as a subsequence of string a.

Code:

#include <stdio.h>
#include <string.h>
int main (void)
{
    char s1[100010],s2[100010];
    scanf("%s",s1);
    scanf("%s",s2);
    int n1=strlen(s1);
    int n2=strlen(s2);
    if (n1>n2)
        printf("%d\n",n1);
    else if (n1<n2)
        printf("%d\n",n2);
    else
    {
        if (strcmp(s1,s2)==0)
            printf("-1\n");
        else
            printf("%d\n",n1);
    }
    return 0;
}

// 如果兩個字串長度不等,則輸出較長字串的長度。

// 當兩個字串長度相等時,如果兩個字串為同一字串,則輸出 -1,否則輸出字串長度。

B - b

Mahmoud has n line segments, the i-th of them has length ai. Ehab challenged him to use exactly 3 line segments to form a non-degenerate triangle. Mahmoud doesn’t accept challenges unless he is sure he can win, so he asked you to tell him if he should accept the challenge. Given the lengths of the line segments, check if he can choose exactly 3 of them to form a non-degenerate triangle.

Mahmoud should use exactly 3 line segments, he can’t concatenate two line segments or change any length. A non-degenerate triangle is a triangle with positive area.

Input:

The first line contains single integer n (3 ≤ n ≤ 105) — the number of line segments Mahmoud has.

The second line contains n integers a1, a2, …, an (1 ≤ ai ≤ 109) — the lengths of line segments Mahmoud has.

Output:

In the only line print “YES” if he can choose exactly three line segments and form a non-degenerate triangle with them, and “NO” otherwise.

Example:

Input:

5
1 5 3 2 4

Output:

YES

Input:

3
4 1 2

Output:

-1

Note:

For the first example, he can use line segments with lengths 2, 4 and 5 to form a non-degenerate triangle.

Code:

#include <stdio.h>
#include <algorithm>            //呼叫sort函式需要的標頭檔案
using namespace std;            //標頭檔案<algorithm>為C++標準,所以要加這句
int main (void)
{
    int n;
    int a[100010];
    scanf("%d",&n);
    for (int i=0;i<n;i++)
        scanf("%d",&a[i]);

    sort(a,a+n);                //預設為從小到大排序
    for (int i=0;i<n-2;i++)
        if (a[i]+a[i+1]>a[i+2]) //判斷三角形時,只要最大一邊小於其餘兩邊之和即可
        {
            printf("YES\n");
            return 0;
        }
    printf("NO\n");
    return 0;
}

// sort(begin,end,cmp);

// begin,end 為排序範圍,cmp為比較函式,不寫則預設為從小到大排序。若要從大到小排序,可寫為

bool cmp(int a,int b)   //寫在main函式之前
{
    return a>b;     //降序排列,如果改為return a<b; 則為升序
}