Scramble Sort

Scramble Sort
Problem description
In this problem you will be given a series of lists containing both words and numbers. The goal is to sort these lists in such a way that all words are in alphabetical order and all numbers are in numerical order. Furthermore, if the nth element in the list is a number it must remain a number, and if it is a word it must remain a word.
Input
The input will contain multiple lists, one per line. Each element of the list will be separated by a comma followed a space, and the list will be terminated by a period. The input will be terminated by a line containing only a single period.

Output
For each list in the input, output the scramble sorted list, separating each element of the list with a comma followed by a space, and ending the list with a period.

Sample Input
0.
banana, strawberry, OrAnGe.
Banana, StRaWbErRy, orange.
10, 8, 6, 4, 2, 0.
x, 30, -20, z, 1000, 1, Y.
50, 7, kitten, puppy, 2, orangutan, 52, -100, bird, worm, 7, beetle.
.
Sample Output
0.
banana, OrAnGe, strawberry.
Banana, orange, StRaWbErRy.
0, 2, 4, 6, 8, 10.
x, -20, 1, Y, 30, 1000, z.
-100, 2, beetle, bird, 7, kitten, 7, 50, orangutan, puppy, 52, worm.

 User: cspark , Language : GNU C++ , Judge Result: Accepted

#include<iostream>
#include<algorithm>
#include<string>

using namespace std;

bool cmp (string a, string b)
{
    int i;
    string s1=a;
    string s2=b;
    for(i=0;i<s1.length();++i)
    {
        if(s1[i]>='a'&&s1[i]<='z') s1[i]=s1[i]-'a'+'A';
    }
    for(i=0;i<s2.length();++i)
    {
        if(s2[i]>='a'&&s2[i]<='z')  s2[i]=s2[i]-'a'+'A';
    }
    int length=s1.length()>s2.length()?s1.length():s2.length();
    for(i=0;i<length;++i)
    {
        if(s1[i]!=s2[i])
        return s1[i]<s2[i]; 
    }
    return false;
}

int main()
{
    string s;
    string str[1000];
    int num[1000];
    int flag[2000];
    int i,f,len,s_len=0,n_len=0,index=0,fla=0;
    memset(flag,0,sizeof(flag));
    while(cin>>s&&s!=".")
    {
        len = s.length()-1;
        if(s[len]=='.')
            fla=1;
        for(i=0,f=0;i<s.length();++i)
        {
            if ((s[i] >= 'A'&&s[i] <='Z')||(s[i]>= 'a'&&s[i] <= 'z'))
            {
                f=1;
                s=s.erase(len,1);
                str[s_len++]=s;
                break;
            }
        }
        if(!f)
        {
            int tmp=0,k=0;
            if(s[0]=='+') continue;
            if(s[0]=='-') k=1;
            for(int p=k ;p<=len-1;++p)
            {
                tmp *= 10;
                tmp += s[p] - 48;    
            }
            if(k) tmp = -tmp;
            num[n_len++]=tmp;
            flag[index]=1;

        }
        ++index;
        if(fla)
        {
            sort(num,num+n_len);
            sort(str,str+s_len,cmp);
            int q=-1,k1=0,k2=0;
            while( 1 )
            {
                ++q;
                if( q >= (n_len+s_len-1)) break;
                if(k1>=n_len)
                {
                    cout<<str[k2++]<<", ";
                    continue;
                }
                if(k2>=s_len)
                {
                    cout<<num[k1++]<<", ";
                    continue;
                }
                if(flag[q]&&k1<n_len)
                    cout<<num[k1++]<<", ";
                else
                    cout<<str[k2++]<<", ";
            }
            if(k1!=n_len) cout<<num[k1]<<".";
            else cout<<str[k2]<<".";
            cout<<endl;
            s_len=0;n_len=0;
            memset(flag,0,sizeof(flag));
            index=0;
            fla=0;
        }
        
    }
    return 0;
}
### 扰码(Scramble)技术原理 扰码是一种用于减少信号中的重复模式的技术,在数据传输过程中广泛应用。该方法通过对原始比特流进行伪随机化处理,使得传输的数据更加均匀分布,从而降低直流偏置并提高接收端的同步性能。 #### 工作机制 在发送方,扰码器会根据特定的多项式生成一个伪随机序列,并将其与待发送的信息位按位异或操作。这样可以打乱原有数据结构,防止长时间连续相同电平出现,有助于维持线路特性阻抗稳定以及简化时钟恢复过程[^3]。 对于接收侧而言,则需要相同的初始状态来逆向执行上述逻辑运算以还原出原信息。值得注意的是,为了确保两端能够正确解码,通常会在帧头加入已知图案作为同步标志,以便于初始化本地产生的PN序列发生器。 #### 应用场景 - **无线通信领域**:如Wi-Fi标准IEEE 802.11n/ac/ax中均采用了不同形式的加扰方案; - **有线网络接口**:例如USB 3.x、PCI Express等高速串行总线协议也引入了类似的机制; - **存储设备间连接**:像SAS (Serial Attached SCSI),它利用特殊的有序集合作为前导符来进行预编码控制。 ```c++ // C++代码示例展示简单的LFSR实现 #include <iostream> using namespace std; class Lfsr { public: unsigned int state; void next() { bool newBit = ((state >> 0) ^ (state >> 2) ^ (state >> 3) ^ (state >> 5)) & 1; // 使用给定反馈函数计算新bit state = (state >> 1) | (-newBit << 7); // 更新寄存器内容并将新bit移入最高位 } }; int main(){ Lfsr lfsr{0xACE1}; // 初始化状态 cout << "Initial State:" << hex << uppercase << lfsr.state << endl; for(int i=0;i<16;++i){ lfsr.next(); cout << dec << i+1 << ": " << hex << uppercase << lfsr.state << endl; } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值