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

The 2006 Benelux Algorithm Programming Contest




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

#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;
}


你可能感兴趣的:(poj3055 Digital Friends)