Daliy Algorithm (dp,模拟)-- day 80
Nothing to fear
种一棵树最好的时间是十年前,其次是现在!
那些你早出晚归付出的刻苦努力,你不想训练,当你觉的太累了但还是要咬牙坚持的时候,那就是在追逐梦想,不要在意终点有什么,要享受路途的过程,或许你不能成就梦想,但一定会有更伟大的事情随之而来。 mamba out~
2020.5.16
人一我十,人十我百,追逐青春的梦想,怀着自信的心,永不言弃!
PAT-A1022
经验:
-
如果getline(cin,xxx)之前出现别的scanf / cin 需要一个
getchar吸收掉之前的回车!因为之前的回车还保留在缓冲区内 -
PAT 将场上卡坑的数据比如输出几位数字7位必须输出前导0
-
考虑每一个数据的对应特殊情况
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <string>
using namespace std;
#define SN std::string::npos
int n , m;
struct book{
int id;
string book_title;
string author;
string keywords;
string publisher;
string published_year;
};
vector<book> a;
bool cmp(book a,book b)
{
return a.id < b.id;
}
void query(int way,string key)
{
bool flag = 0;
printf("%d: ",way);cout << key << endl;
for(int i = 0;i < a.size() ;i ++)
{
if(way == 1)
{
if(a[i].book_title == key)
{
printf("%07d\n", a[i].id);
flag = 1;
}
}
if(way == 2)
{
if(a[i].author == key)
{
flag = 1;
printf("%07d\n", a[i].id);
}
}
if(way == 3)
{
if(a[i].keywords.find(key) != SN)
{
flag = 1;
printf("%07d\n", a[i].id);
}
}
if(way == 4)
{
if(a[i].publisher == key)
{
flag = 1;
printf("%07d\n", a[i].id);
}
}
if(way == 5)
{
if(a[i].published_year == key)
{
flag = 1;
printf("%07d\n", a[i].id);
}
}
}
if(!flag)cout << "Not Found" << endl;
}
void input()
{
cin >> n;
int aa ;string b ,c ,d ,e,f;
for(int i = 0;i < n ;i ++)
{
cin >> aa;getchar();getline(cin,b);
getline(cin,c);getline(cin,d);
getline(cin,e);cin >> f;
a.push_back({aa,b,c,d,e,f});
}
sort(a.begin(),a.end(),cmp);
cin >> m;
int choice ;string key;
for(int i = 0;i < m;i ++)
{
scanf("%d: ",&choice);
getline(cin,key);
query(choice , key);
}
}
int main()
{
input();
return 0;
}
下面是dp复习
Constanze's Machine
通常遇到和前两种状态或者两种字符状态相关的最值问题,首先是动态规划问题,其次考虑斐波那契数列的变形。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <cassert>
#include <string>
#include <iomanip>
#include <cmath>
#include <ctime>
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define lowbit(x) (x & -x)
using namespace std;
typedef long long ll;
const int MAX = 0x7ffffff;
const int mod = 1e9 + 7;
int t;
void slove()
{
string s;
cin >> s;
s = '0' + s;
int n = s.length();
vector<ll> f(n , 0);
f[0] = 1 , f[1] = 1;
for(int i = 1; i <= n;i ++)
{
if(s[i] == 'm' || s[i] == 'w'){
cout << 0 << endl;return;
}
f[i] = f[i-1];
if(s[i] == 'u')
{
if(s[i-1] == 'u')f[i] = f[i-1] + f[i-2];
else f[i] = f[i-1];
}
else if(s[i] == 'n')
{
if(s[i-1] == 'n')f[i] = f[i-1] + f[i-2];
else f[i] = f[i-1];
}
f[i] = f[i] % mod;
}
cout << f[n] << endl;
}
int main()
{
#ifdef LOCAL
auto start_time = clock();
cerr << setprecision(3) << fixed; // 在iomanip中
#endif
SIS;
slove();
#ifdef LOCAL
auto end_time = clock();
cerr << "Execution time: " << (end_time - start_time) * (int)1e3 / CLOCKS_PER_SEC << " ms\n";
#endif
}
Basketball Exercise
关键在于对于f3的状态的计算是从排除掉当前f1和f2所包含的阶段状态!
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <cassert>
#include <string>
#include <iomanip>
#include <cmath>
#include <ctime>
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define lowbit(x) (x & -x)
using namespace std;
typedef long long ll;
const int MAX = 0x7ffffff;
int t;
void slove()
{
int n;
cin >> n;
vector<ll> a(n + 1),b(n + 1);
for(int i = 1;i <= n ;i ++)cin >> a[i];
for(int i = 1;i <= n ;i ++)cin >> b[i];
vector<ll> f1(n + 1),f2(n+1),f3(n+1);
f1[1] = a[1];f2[1] = b[1];
for(int i = 2;i <= n ;i ++)
{
f1[i] = max(max(f2[i-1],f3[i-1]) + a[i], a[i]);
f2[i] = max(max(f1[i-1],f3[i-1]) + b[i], b[i]);
f3[i] = max(f1[i-1],f2[i-1]);
}
cout << max(f1[n],f2[n]) << endl;
}
int main()
{
#ifdef LOCAL
auto start_time = clock();
cerr << setprecision(3) << fixed; // 在iomanip中
#endif
SIS;
slove();
#ifdef LOCAL
auto end_time = clock();
cerr << "Execution time: " << (end_time - start_time) * (int)1e3 / CLOCKS_PER_SEC << " ms\n";
#endif
}
LG-P1435 回文字串
关键在于将问题转化为LCS问题去解决。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1005;
char a[N] , b[N];
int f[N][N];
int main()
{
scanf("%s",a + 1);
int n = strlen(a + 1);
for(int i = 1 ;i <= n; i++)b[i] = a[n-i+1];
for(int i = 1;i <= n ;i ++)
{
for(int j = 1;j <= n ;j ++)
{
f[i][j] = 1;
if(a[i] == b[j])f[i][j] = f[i-1][j-1] + 1;
else f[i][j] = max(f[i-1][j],f[i][j-1]);
}
}
cout << n - f[n][n] << endl;
return 0;
}