地址:http://codeforces.com/contest/766/problem/A

A题:

A. Mahmoud and Longest Uncommon Subsequence
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples
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.

思路:最大的连续不同子串肯定是整个串,如果两个串相同则是-1。

 #include <bits/stdc++.h>

 using namespace std;

 #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e6+;
const int mod=1e9+; char s1[K],s2[K]; int main(void)
{
cin>>s1>>s2;
if(strcmp(s1,s2)==)
printf("-1\n");
else
printf("%d\n",max(strlen(s1),strlen(s2)));
return ;
}

B题:

B. Mahmoud and a Triangle
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples
input
5
1 5 3 2 4
output
YES
input
3
4 1 2
output
NO
Note

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

思路:判三角形成立的条件,两边之和大于第三边,两边之差小于第三边。枚举边肯定不行,时间复杂度太高。

  所以可以先从小到大排个序,然后判断第i-1条边和第i条边之和是否大于第i+1条边即可,因为第i-1条边和第i条边之差必定小于第i+1条边。

  这样扫一遍即可,复杂度O(nlogn)。

 #include <bits/stdc++.h>

 using namespace std;

 #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e6+;
const int mod=1e9+; int a[K]; int main(void)
{
int n,ff=;
cin>>n;
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
sort(a+,a++n);
for(int i=;i<n&&!ff;i++)
if(a[i-]+a[i]>a[i+])
ff=;
if(ff)
printf("YES\n");
else
printf("NO\n");
return ;
}

Codeforces Round #396 (Div. 2) A - Mahmoud and Longest Uncommon Subsequence B - Mahmoud and a Triangle的更多相关文章

  1. Codeforces Round #396 (Div. 2) A. Mahmoud and Longest Uncommon Subsequence 水题

    A. Mahmoud and Longest Uncommon Subsequence 题目连接: http://codeforces.com/contest/766/problem/A Descri ...

  2. Codeforces Round #396 (Div. 2) A,B,C,D,E

    A. Mahmoud and Longest Uncommon Subsequence time limit per test 2 seconds memory limit per test 256 ...

  3. Codeforces Round #396 (Div. 2) A B C D 水 trick dp 并查集

    A. Mahmoud and Longest Uncommon Subsequence time limit per test 2 seconds memory limit per test 256 ...

  4. 766A Mahmoud and Longest Uncommon Subsequence

    A. Mahmoud and Longest Uncommon Subsequence time limit per test 2 seconds memory limit per test 256 ...

  5. Codeforces766A Mahmoud and Longest Uncommon Subsequence 2017-02-21 13:42 46人阅读 评论(0) 收藏

    A. Mahmoud and Longest Uncommon Subsequence time limit per test 2 seconds memory limit per test 256 ...

  6. Codeforces Round #396 (Div. 2) A

    While Mahmoud and Ehab were practicing for IOI, they found a problem which name was Longest common s ...

  7. 【codeforces 766A】Mahmoud and Longest Uncommon Subsequence

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary 并查集

    D. Mahmoud and a Dictionary 题目连接: http://codeforces.com/contest/766/problem/D Description Mahmoud wa ...

  9. Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary

    地址:http://codeforces.com/contest/766/problem/D 题目: D. Mahmoud and a Dictionary time limit per test 4 ...

随机推荐

  1. 阐述二维码的原理以及使用google api和PHP QR Code来生成二维码

    一.什么是二维码:二维码 (2-dimensional bar code),是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的.在许多种类的二维条码中,常用的码 ...

  2. 【vijos】1881 闪烁的繁星(线段树+特殊的技巧)

    https://vijos.org/p/1881 这场比赛太难了sad.所以我都没做.. 这题一开始我竟然不会sad(本来就不会),然后我继续yy..yy了好久,竟然yy了个什么可拆分的并查集?(sa ...

  3. 使用JSR验证

    1.spring mvc配置文件中添加:         <mvc:annotation-driven /> 2.pom.xml中添加        <dependency > ...

  4. 【ask】ghost分区还原win7出现蓝屏,试图加载CLASSPNP驱动时出现

    1.最近由于需要把整个c盘备份了,然后还原在另一台机器上. 2.结果当然时出事了,开始一切如旧,但是机器重启后,蓝屏了. 3.哦,开始立马觉得可能时因为有些软件的驱动是装载d盘了. 4.赶紧把d盘也备 ...

  5. js数组的方法

    arrayObject.join(separator) 将数组以separator字符为间隔转化为字符串并返回,如果不传,默认为逗号.此方法不会改变原数组 let arr = [1,2,3]; arr ...

  6. const在指针中的用法

    一.指向const对象的指针---对象不能修改 方式1 int value1 = 3; const int *p1 = &value1; *p1 = 5; //错误,不能修改const指向对象 ...

  7. XAMPP修改mysql的默认密码的三种方法

     How I can set a "root" password in MySQL? (method 1) In the \xampp\mysql\bin directory en ...

  8. Wex5循环某个div节点下的所有img标签,替换其src属性值

    Model.prototype.newsDetailAfterRefresh = function(event){ var newsDiv = this.getElementByXid("n ...

  9. 《从零开始学Swift》学习笔记(Day60)——Core Foundation框架

    原创文章,欢迎转载.转载请注明:关东升的博客 Core Foundation框架是苹果公司提供一套概念来源于Foundation框架,编程接口面向C语言风格的API.虽然在Swift中调用这种C语言风 ...

  10. Python全栈day24(面向对象编程作业作业_定义学校老师课程班级学生类)

    面向对象作业 作业_定义学校老师课程班级学生类.py #面向对象编程作业,定义学校老师课程班级学生类 #定义几个类,尽可能定义多的数据属性及函数属性 class School: def __init_ ...