A Mahmoud and Longest Uncommon Subsequence
解题思路整理:
最长不相同串,思路很简单,如果两个串相同,则肯定不存在不相同串,如果两个串不相同,则取最长串的长度~
解题代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(int i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
typedef vector<int> vi;
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
const int N = 1e7+5;
string a,b;
int main()
{
cin >> a>>b;
if(a==b)
{
cout << "-1"<<endl;
}
else
{
cout << max(a.size(),b.size())<<endl;;
}
return 0;
}
B - Mahmoud and a Triangle
解题思路整理:
贪心算法:
首先排序,相邻三个数相差最小,只判断相邻三个数是否可以组成三角形,然后1-n暴力搜一遍
解题代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(int i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
typedef vector<int> vi;
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
const int N = 1e7+5;
int n,t;
vi a;
int main()
{
cin >>n;
rep(i,1,n)
{
cin >> t;
a.push_back(t);
}
sort(a.begin(),a.end());
for(int i=0;i<n-2;i++)
{
if(a[i]+a[i+1]>a[i+2]&&a[i+1]+a[i+2]>a[i]&&a[i]+a[i+2]>a[i+1])
{
cout << "YES"<<endl;
return 0;
}
}
cout << "NO"<<endl;
return 0;
}
C. Mahmoud and a Message 划分DP
解题思路整理:
简单dp:
我首先想到的是区间dp,但是时间复杂度为O(n^3),所以超时,
后来看到题解为简单dp,时间复杂度为O(n^2).一个dp[]数组,从右到左根据划分遍历。
解题代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(int i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
typedef vector<int> vi;
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
#define MOD (ll)(1e9+7)
const int N = 1e7+5;
bool judge(int l,int r);
string s;
ll n;
int a[30];
int re=0;
ll dp[1005];
ll dp1[1005];
int main()
{
cin >>n;
cin >> s;
rep(i,0,25)
{
cin >> a[i];
}
dp1[1]=1;
dp[0]=1;
for(int len=1;len<=n;len++)
{
dp1[len] = inf_int;
for(int i=1;i<=len;i++)
{
if(judge(i,len))
{
dp[len] += dp[i-1]%MOD;
dp[len] %= MOD;
dp1[len] = min(dp1[len],dp1[i-1]+1);
}
}
}
cout << dp[n]<<endl;
cout <<re<<endl;
cout <<dp1[n]<<endl;
return 0;
}
bool judge(int l,int r)
{
int len = r-l+1;
int ct=a[s[l-1]-'a'];
for(int kk=l;kk<=r;kk++)
{
ct = min(ct,a[s[kk-1]-'a']);
}
if(ct>=len)
{
if(len>re)
re = len;
// cout << l<<" "<<r<<endl;
return true;
}
else
return false;
}