CSUFT 2018年个人赛(1) F题(51nod-1347)及 H题 (hdu-1789)

本文解析了两道算法题:一是通过旋转判断字符串是否为对称串的问题,二是安排作业顺序以最小化扣分的贪心算法实现。文章提供了完整的代码示例及解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先补两题 第一次个人赛的题目 都是水题~

        旋转字符串

S0...n10...n−1是一个长度为n的字符串,定义旋转函数Left(S)=S1n11…n−1+S00.比如S=”abcd”,Left(S)=”bcda”.一个串是对串当且仅当这个串长度为偶数,前半段和后半段一样。比如”abcabc”是对串,”aabbcc”则不是。

现在问题是给定一个字符串,判断他是否可以由一个对串旋转任意次得到。


Input第1行:给出一个字符串(字符串非空串,只包含小写字母,长度不超过1000000)Output对于每个测试用例,输出结果占一行,如果能,输出YES,否则输出NO。Sample Input
aa
ab
Sample Output
YES
NO

F题 意思就是判断 一个字符串长度是否为偶数 再判断 该字符串的前一半与后一半是否相等

代码很简单的:

#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
char c[1000005];
bool flag;
int main()
{
    while (~scanf("%s",c))
    {
        flag=true;
        int len=strlen(c);
        if (len%2!=0)
            flag=false;
        else
            for (int i=0; i<len/2; i++)
                if (c[i]!=c[i+len/2])
                {
                    flag=false;
                    break;
                }
        if (flag==true)
            puts("YES");
        else puts("NO");
    }
    return 0;
}

放了原题的样子...可能我更喜欢hdu的界面

Doing Homework again

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 16626    Accepted Submission(s): 9695


Problem Description
Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will reduce his score of the final test. And now we assume that doing everyone homework always takes one day. So Ignatius wants you to help him to arrange the order of doing homework to minimize the reduced score.
 

Input
The input contains several test cases. The first line of the input is a single integer T that is the number of test cases. T test cases follow.
Each test case start with a positive integer N(1<=N<=1000) which indicate the number of homework.. Then 2 lines follow. The first line contains N integers that indicate the deadlines of the subjects, and the next line contains N integers that indicate the reduced scores.
 

Output
For each test case, you should output the smallest total reduced score, one line per test case.
 

Sample Input

 
333 3 310 5 131 3 16 2 371 4 6 4 2 4 33 2 1 7 6 5 4
 

Sample Output

 
035
 

H题 题意为一个人要在规定时间内完成尽可能多的作业 保证扣的学分的最少

在看题目的时候就想到要用dp

但是 没考虑到如果当天已经做过作业 要将完成时间前移到 前面没有写过作业的日子

时间也不够 只构造了结构体以及sort函数 没继续写下去


后来讲题的时候提到了是用贪心算法

就去查了一下资料

是某乎上的回答  点击打开链接 

贪心算法(Greedy algorithm): 从问题解题过程一步一步按照某个策略取当前的最优值,并且达到全局最优结果;
动态规划(DP):在解决当前问题的时候考虑到之前所有的结果使得过去最优。因此动态规划得到的是每时每刻的全局最优解,并且逐步扩大问题规模。动态规划的精髓是组合子问题


作者:知乎用户
链接:https://www.zhihu.com/question/36662980/answer/68494301
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


有机会的话 希望能做一个专题...

放一下代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
struct node
{
    int time,mark;
    node () {}
    node (int a,int b)
    {
        time =a;
        mark =b;
    }
} m[2005];
bool operator <(node m,node n)//内置重载
{
    return m.mark>n.mark;
}
int main()
{
    int t,n;
    int x[2005];
    scanf("%d",&t);
    for (int i=0; i<t; i++)
    {
        scanf("%d",&n);
        int sum=0;
        for (int j=0; j<n; j++)
        {
            scanf("%d",&m[j].time);
        }
        for (int j=0; j<n; j++)
        {
            scanf("%d",&m[j].mark);
        }
        sort(m,m+n);
        memset(x,0,sizeof x);
        for (int k=0; k<n; k++)
        {
            int flag=0;
            for (int j=m[k].time; j>0; j--)
            {
                if (x[j]==0)
                {
                    x[j]=1;
                    flag++;
                    break;
                }
            }
            if (flag==0)
                sum+=m[k].mark;
        }
        printf("%d\n",sum);
    }
    return 0;
}

本来是乖乖地写了 cmp函数  放到 sort 里面

但是后来在别人的博客上看到了 operator 函数内置重载 就想试一下 先记住这种形式 以后再研究

先这样吧 先溜去补题了 改天再改内容


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值