poj3055 Digital Friends

Digital Friends
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1081 Accepted: 505

Description

Two positive integers are called friends if they consist of the same decimal digits. So 123 and 32331313323213 are friends, but 123 and 22121221 are not. 

Two positive integers (that are not friends) are called almost friends if a single neighbour exchange in one of them results in a pair of friends. A neighbour exchanges two neighbouring digits a and b into a-1 and b+1, or into a+1 and b-1, provided that these new digits are still in the range 0...9, and that no leading zero is generated. So 123 and 2223042 are almost friends(let 04->13), and 137 and 470 are neither friends nor almost friends(note that 13 -> 04 is not allowd). 
The problem is to determine if two given integers are friends or almost friends.

Input

The frist line of the input contains a single number: the number of test cases to follow. Each test case has the following format: 
One line with two integer x and y, separated by a single space, with 0 < x, y < 10 100.Both integers start with a non-zero digit. 

Output

For every test case in the input, the output should contain a single line with the string "friends" or "almost friends" or "nothing", reflecting the property of the two given integers 

Sample Input

4
123 32331313323213
123 22121221
123 223042
137 470

Sample Output

friends
almost friends
almost friends
nothing

Source





简单水题,就是处理起来写的比较难看罢了。

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

#define MAXN 500

char a[MAXN],b[MAXN];
int visa[15],visb[15];

bool checkEqual()
{
    for (int i=0;i<=10;i++)
    {
        if (visa[i]==0 && visb[i]!=0 || visa[i]!=0 && visb[i]==0) return false;
    }
    return true;
}

int main()
{
    int i,j,n,T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s%s",a,b);
        int na=strlen(a);
        int nb=strlen(b);
        memset(visa,0,sizeof(visa));
        memset(visb,0,sizeof(visb));
        for (i=0;i<na;i++)
        {
            visa[a[i]-'0']++;
        }
        for (i=0;i<nb;i++)
        {
            visb[b[i]-'0']++;
        }
        if (checkEqual())
        {
            printf("friends\n");
            continue;
        }
        for (i=0;i<na-1;i++)
        {
            if (a[i]-'0'>0)
            {
                visa[a[i]-'0']--;
                visa[a[i]-'0'-1]++;
                visa[a[i+1]-'0']--;
                visa[a[i+1]-'0'+1]++;
                if (checkEqual() && (i!=0 || a[i]!='1')) break;
                visa[a[i]-'0']++;
                visa[a[i]-'0'-1]--;
                visa[a[i+1]-'0']++;
                visa[a[i+1]-'0'+1]--;
            }
            if (a[i+1]-'0'>0)
            {
                visa[a[i]-'0']--;
                visa[a[i]-'0'+1]++;
                visa[a[i+1]-'0']--;
                visa[a[i+1]-'0'-1]++;
                if (checkEqual()) break;
                visa[a[i]-'0']++;
                visa[a[i]-'0'+1]--;
                visa[a[i+1]-'0']++;
                visa[a[i+1]-'0'-1]--;
            }
        }
        if (i<na-1)
        {
            printf("almost friends\n");
            continue;
        }
        for (i=0;i<nb-1;i++)
        {
            if (b[i]-'0'>0)
            {
                visb[b[i]-'0']--;
                visb[b[i]-'0'-1]++;
                visb[b[i+1]-'0']--;
                visb[b[i+1]-'0'+1]++;
                if (checkEqual() && (i!=0 || b[i]!='1')) break;
                visb[b[i]-'0']++;
                visb[b[i]-'0'-1]--;
                visb[b[i+1]-'0']++;
                visb[b[i+1]-'0'+1]--;
            }
            if (b[i+1]-'0'>0)
            {
                visb[b[i]-'0']--;
                visb[b[i]-'0'+1]++;
                visb[b[i+1]-'0']--;
                visb[b[i+1]-'0'-1]++;
                if (checkEqual()) break;
                visb[b[i]-'0']++;
                visb[b[i]-'0'+1]--;
                visb[b[i+1]-'0']++;
                visb[b[i+1]-'0'-1]--;
            }
        }
        if (i<nb-1)
        {
            printf("almost friends\n");
            continue;
        }
        printf("nothing\n");
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值