zoukankan      html  css  js  c++  java
  • NYOJ 5,7

    总要刷点水题找自信Orz

    //5 KMP

    //7 绝对值加和最小

    //NYOJ 5 简单题
    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    using namespace std;
    const int N = 1000 + 5;
    char s[15];
    char ss[N];
    int nxt[15];
    void getnxt()
    {
        int n = strlen(s);
        int i = 0, j = -1;
        nxt[0] = -1;
        while(i < n)
        {
            if(j == -1 || s[i] == s[j])
            {
                i++;j++;
                if(s[i] == s[j])
                    nxt[i] = nxt[j];
                else
                    nxt[i] = j;
            }
            else
                j = nxt[j];
        }
    }
    
    int kmp()
    {
        int i = 0, j = 0;
        int cnt = 0;
        int n = strlen(ss), m = strlen(s);
        while(i < n && j < m)
        {
            if(j == -1 || ss[i] == s[j])
            {
                i++; j++;
            }
            else
                j = nxt[j];
            if(j == m)
            {
                cnt++;
                j = nxt[j];
            }
        }
        return cnt;
    }
    
    int t;
    int main()
    {
        scanf("%d", &t);
        while(t--)
        {
            scanf("%s", s);
            scanf("%s", ss);
            getnxt();
            printf("%d
    ",kmp());
        }
    
    }
    //NYOJ 7 简单题
    #include <stdio.h>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    const int N = 20 + 5;
    int x[N], y[N];
    int t, n;
    
    int main()
    {
        scanf("%d", &t);
        while(t--)
        {
            scanf("%d", &n);
            for(int i = 0; i < n; i++)
            {
                scanf("%d%d", &x[i], &y[i]);
            }
            int ans = 0;
            sort(x, x+n);
            sort(y, y+n);
            for(int i = 0, j = n-1; i < j; i++, j--)
            {
                ans += x[j]-x[i];
                ans += y[j]-y[i];
            }
            printf("%d
    ", ans);
        }
    
    }
    如果有错误,请指出,谢谢
  • 相关阅读:
    mysql高并发配置
    php xml转array的方法
    双系统,一系统损坏后的解决方案之硬盘启动
    最长公共前缀
    罗马数字转整数
    回文数
    整数反转
    一、数组---两数之和
    从尾到头打印链表
    替换空格
  • 原文地址:https://www.cnblogs.com/Alruddy/p/7282751.html
Copyright © 2011-2022 走看看