题目:
【输入形式】
输入为3行:
第1行:线性移位寄存器的阶数n;
第2行:反馈函数的系数c1,c2,...,cn;
第3行:初始状态a0,a1,...,an-1。
【输出形式】
输出为2行:
第1行:为周期p;
第2行:输出序列(输出n+p位)。
【样例输入】
5
10010
10011
【样例输出】
31
100110100100001010111011000111110011
程序代码:
#include <iostream>
#include <string.h>
using namespace std;
char c[100];
char a[100];
char b[100];
char a1[100];
int n,x;
void yiwei(int n){
int i;
for(i=1;i<n;i++){
a1[i-1]=a1[i];
}
}
int yihuo(int x){
int i;
char temp;
temp=a1[b[0]];
for(i=1;i<=x;i++){
temp=((temp-48)^(a1[b[i]]-48))+48;
}
return temp;
}
int zhouqi(){
int t;
t=strcmp(a,a1);
if(t==0){
return 0;
}else{
return 1;
}
}
int main(){
int n;
cin>>n;
char out[100];
char temp;
int x=-1;
int i;
int p;
cin>>c;
cin>>a;
for(i=0;i<n;i++){
if(c[i]=='1') {
x++;
b[x]=i;
}
}
out[0]=a[0];
int j=0;
for(i=1;i<n;i++){
a1[i-1]=a[i];
}
temp=a[b[0]];
for(i=1;i<=x;i++){
temp=((temp-48)^((a[b[i]]-48)))+48;
}
a1[n-1]=temp;
while(zhouqi()){
j++;
out[j]=a1[0];
temp=yihuo(x);
yiwei(n);
a1[n-1]=temp;
}
p=j+1;
cout<<p<<endl;
for(i=0;i<p+n;i++){
cout<<out[i%p];
}
return 0;
}