算法设计课程的作业,搜了一下发现没有相关的,就在这里记录一下。还不是很完善(待完善。。。)
# include<iostream>
# include<string>
# include<string.h>
using namespace std;
int main(){
/*设置最小密度*/
int a;
cout<<"输入最小密度:";
cin>> a;
//cout<<a;
/*定义2终端*/
int P[13],Q[13];
for(int i = 0; i < 13; i++){
P[i] = i+1;
Q[i] = i+1;
}
/*
for(int i = 0; i < 13; i++){
cout<<P[i]<<" ";
cout<<Q[i] ;
}
*/
/*输入标记的终端*/
cout << "输入标记的终端:" <<endl;
cout << "P:";
string q_str, p_str, p_BiaoJi[13], q_BiaoJi[13];
cin >> p_str;
/*for(int i = 0; i<p_str.length(); i++) //测试p_str
cout<<p_str[i];
cout<<endl; */
cout << "Q:";
cin >> q_str;
cout<<"要标记的终端为:"<<endl;
for(int i=0; i<p_str.length(); i++)
cout<<"P"<<p_str[i] <<" ";
//p_BiaoJi[i] =
cout<<endl;
for(int i=0; i<q_str.length(); i++)
cout<<"Q"<<q_str[i] <<" ";
cout<<endl;
/*遍历、连接P和Q*/
string result[p_str.length()][2];
int zhui_xian,temp = 1;
for(int i = 0; i<p_str.length(); i++){
result[i][0] = "P" + to_string(p_str[i] - '0');
result[i][1] = "Q" + to_string(q_str[i] - '0');
}
if(p_str[0] - '0' > q_str[0] - '0'){
zhui_xian = p_str[0] - '0';
int temp2=0;
while(temp2<13){
for(int k=q_str[0] - '0';k<zhui_xian;k++)
if(q_str[k] - '0' <= zhui_xian)
temp++;
//cout<<"temp:"<<temp<<endl;
if(temp > a)
for(int i = 1; i<p_str.length(); i++)
result[i][1] = "Q" + to_string(q_str[i+temp-a] - '0');
temp2++;
temp=1;
}
}
for(int i = 0; i<p_str.length(); i++)
cout<<"("<<result[i][0]<<","<<result[i][1] <<")"<<endl;
return 0;
}