个人赛11 div.2总结

一看题集是太平洋西北地区的比赛题,还想着比赛题怎么会分到div.2里,后来才发现正规比赛都分div12。。 大部分还是简单题,难度不太大

Alphabet
A string of lowercase letters is called alphabetical if deleting zero or more of its letters can result in the alphabet string “abcdefghijklmnopqrstuvwxyz”.
Given a string s, determine the minimum number of letters to insert anywhere in the string to make it alphabetical.

Input
The input consists of a single line containing the string s (1 ≤ |s| ≤ 50).
It is guaranteed that s consists of lowercase ASCII letters ‘a’ to ‘z’ only.

Output
Print, on a single line, a single integer indicating the minimum number of letters that must be inserted in order to make the string s alphabetical.

Sample Input Sample Output
xyzabcdefghijklmnopqrstuvw 3
Sample Input Sample Output
aiemckgobjfndlhp 20

解题思路 典型dp题,刚开始方程已经推出来了但是演算的时候算错了数浪费了很长时间。

#include <algorithm>
#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <cstring>
#include <set>
#include <string>
using namespace std;


int main()
{
    char s[100];
    int dp[100];
    int l,Max=1;
    for(int i=0;i<100;i++)
        dp[i]=1;
    fgets(s,100,stdin);
    l=strlen(s);
    for(int i=0; i<l-1; i++)
    {
        for(int j=0;j<i;j++)
        {
            if(s[j]<s[i])
                dp[i]=max(dp[i],dp[j]+1);
        }
        if(dp[i]>Max)
            Max=dp[i];
    }
    cout<<26-Max<<endl;

    return 0;
}

Zigzag
Your Ph.D. thesis on properties of integer sequences is coming along nicely. Each chapter is on a
different type of sequence. The first covers arithmetic sequences. Subsequently you cover binomial
sequences, computable sequences, decreasing sequences, and so on. You have one more chapter to
write, on zigzagging sequences.
A sequence is zigzagging if adjacent elements alternate between strictly increasing and strictly
decreasing. The first pair of numbers can be either strictly increasing or strictly decreasing.
For a given sequence, find the length of its longest zigzagging subsequence.
Input
The first line of input contains a single integer n (1 ≤ n ≤ 50), the length of the sequence.
The second line contains n space-separated integers, describing the sequence.
Every number in the sequence is guaranteed to be between 1 and 50, inclusive.
Output
Print, on a single line, the length of a longest zigzagging subsequence of the input sequence.
Sample Input Sample Output
5 4
2 1 3 4 2

解题思路 同是一道dp题,找出序列中最大的曲折子序列,与上题类似,可以开一个二维dp数组,分别表示一个数在上和下两种状态的dp值,分情况计算即可。

#include <algorithm>
#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <cstring>
#include <set>
#include <string>
using namespace std;


int main()
{
    int a[100];
    int dp[100][2];
    int n,Max=1;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>a[i];
        dp[i][0]=dp[i][1]=1;
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<i;j++)
        {
            if(a[j]>a[i])
                dp[i][0]=max(dp[i][0],dp[j][1]+1);
            else if(a[j]<a[i])
                dp[i][1]=max(dp[i][1],dp[j][0]+1);
        }
        Max=max(Max,max(dp[i][0],dp[i][1]));
    }

    cout<<Max<<endl;

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值