HNU暑假赛七B

Primordial Values
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB
Total submit users: 72, Accepted users: 68
Problem 12877 : No special judgement
Problem description

What's the next number in the following sequence?
1, 11, 21, 1211, 111221, ...
Most people assume that the sequence has something to do with numeric values and struggle to find a pattern . In fact, the rule for generating the next value in the series is that each value is a description of the previous value. For example, 21 can be described as "one 2, one 1", leading to the next value of 1211. By this rule, the answer to the problem above is 312211 ("three 1, two 2, one 1").
The process can also be reversed to determine previous values in the sequence. For example, the value that comes before 2221 (two 2, two 1) would be 2211, and the value before that would be 221. However, not every value has a previous value. For example, 221 has no previous value because it isn't a valid description, and 2212 has no previous value because the value it describes (222) would have a next value of 32, not 2212.
The "primordial value" of some value is defined as the value that would generate a sequence containing the given value but that itself has no previous value. For example, if the starting value is 2221 the primordial value is 221, and if the starting value is 312211 the primordial value is 1. As a special case, the value 22 (for which the previous value would also be 22) is considered primordial.
Your task is to write a program that determines primordial values for given starting values.


Input

Input will consist of specifications for a series of tests. Information for each test is a single line containing a string of digits of length 1 <= n < 100 that specifies the last value in a sequence defined as above. A line containing the value 0 terminates the input.


Output

Output should consist of one line for each test comprising the test number (formatted as shown) followed by a single space and the primordial value of the input value.


Sample Input
2221
312211
22
0
Sample Output
Test 1: 221
Test 2: 1
Test 3: 22
模拟题,还算是一般吧,不算是太水,自己用两个队列维护的。代码写的比较水
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<fstream>
#include<algorithm>
#include<string>
#include<stack>
#include<queue>
#include<climits>
#include<map>
#define MAXP(x,y)  (((x)>(y))?(x):(y))
#define MINP(x,y)  (((x)<(y))?(x):(y))
const int MAX=0xfffffff;
using namespace std;
int main( )
{
    //freopen("1.txt","r",stdin);
    char s[110];
    queue<int>q1;
    queue<int>q2;
    int kase=0;
    while(cin>>s&&strcmp("0",s)!=0)
    {
        while(!q1.empty( ))  q1.pop();
        while(!q2.empty( ))  q2.pop();
        int ls=strlen(s);
        if(ls%2==1)
        {
            printf("Test %d: ",++kase);
            puts(s);
            continue;
        }
        for(int i=0;i<ls;i++)
        {
            int t=s[i]-'0';
            q1.push(t);
        }
        int flag1=0;
        int flag2=0;
        while(!flag1&&!flag2)
    {
            int ts1=q1.size( );

        if(ts1%2==0)
            for(int i=0;i<ts1/2;i++)
            {
            int t1=q1.front();
            q1.pop();
            int t2=q1.front();
            q1.pop( );
            if(ts1==2&&t1==2&&t2==2)
            {
                flag1=3;
                break;
            }
            for(int j=1;j<=t1;j++)
                q2.push(t2);
            }
        while(!q1.empty( ))  q1.pop();
        int ts2=q2.size( );
        if(flag1==3)  break;
        if(ts2%2!=0)
        {
            flag2=1;
            break;
        }
        if(ts2%2==0)
            for(int i=0;i<ts2/2;i++)
            {
            int t1=q2.front();
            q2.pop();
            int t2=q2.front( );
            q2.pop( );
            if(ts2==2&&t1==2&&t2==2)
            {
                flag2=3;
                break;
            }
            for(int j=1;j<=t1;j++)
                q1.push(t2);
            }
            ts1=q1.size( );

            if(flag2==3)  break;
            if(ts1%2!=0)
            {
                flag1=1;
                break;
            }
        while(!q2.empty( ))  q2.pop();
    }
        printf("Test %d: ",++kase);
        if(flag1==3||flag2==3)
            cout<<"22\n";
        else if(flag1==1)
            while(!q1.empty())
            {
                cout<<q1.front();
                q1.pop();
            }
        else if(flag2==1)
            while(!q2.empty())
            {
                cout<<q2.front();
                q2.pop();
            }
        cout<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值