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\n",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\n", ans);
    }

}
posted @ 2017-08-04 00:01  可达龙  阅读(153)  评论(0编辑  收藏  举报