主题:[讨论]请教个程序,帮忙解读下!!
// Example2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
//Add code
#include<string>
#include<iostream>
//定义一个名空间
using namespace std;
struct record
{
int number,value;
string str;
};
struct record word[150][150];
int main(int argc, char* argv[])
{
//Add code
string str,str1,str2;
char temp[8];
int i,j,k,l,value;
int min,o,p,x,y;
int list[150][150];
int length[150];
for (i=0;i<150;i++)
{
length[i]=0;
}
for (i=2;i<150;i++)
for (j=1;j<i;j++)
{
if (i%j==0)
{
list[i][length[i]++]=j;
}
}
while(cin>>str)
{
l=str.length();
for (i=0;i<l;i++)
{
if (i==0)
{
for (j=0;j<l;j++)
{
word[j][i].number=word[j][i].value=1;
word[j][i].str=str[j];
}
}
else
{
for (j=0;j<l-i;j++)
{
min=1000;
for (k=0;k<i;k++)
{
if (word[j][k].number==1)
str1=word[j][k].str;
else
{
sprintf(temp,"%d",word[j][k].number);
str2=temp;
str1=str2+"("+word[j][k].str+")";
}
if (word[j+k+1][i-k-1].number==1)
{
str1=str1+word[j+k+1][i-k-1].str;
}
else
{
sprintf(temp,"%d",word[j+k+1][i-k-1].number);
str2=temp;
str1=str1+str2+"("+word[j+k+1][i-k-1].str+")";
}
value=str1.length();
if (value<min)
{
min=value;
word[j][i].number=1;
word[j][i].str=str1;
word[j][i].value=value;
}
}
for (k=0;k<length[i+1];k++)
{
x=list[i+1][k];
y=(i+1)/x;
for (p=0;p<x;p++)
for (o=1;o<y;o++)
{
if (str[j+p]!=str[j+o*x+p])
goto next;
}
sprintf(temp,"%d",y);
str2=temp;
value=word[j][x-1].value+str2.length()+2;
if (value<min)
{
min=value;
word[j][i].number=y;
word[j][i].str=word[j][x-1].str;
word[j][i].value=value;
}
next: ;
}
}
}
}
if (word[0][l-1].number==1)
{
cout<<word[0][l-1].str<<endl;
}
else
{
cout<<word[0][l-1].number<<"("<<word[0][l-1].str<<")"<<endl;
}
}
return 0;
}
谢谢了!!帮忙解读下!!
//
#include "stdafx.h"
//Add code
#include<string>
#include<iostream>
//定义一个名空间
using namespace std;
struct record
{
int number,value;
string str;
};
struct record word[150][150];
int main(int argc, char* argv[])
{
//Add code
string str,str1,str2;
char temp[8];
int i,j,k,l,value;
int min,o,p,x,y;
int list[150][150];
int length[150];
for (i=0;i<150;i++)
{
length[i]=0;
}
for (i=2;i<150;i++)
for (j=1;j<i;j++)
{
if (i%j==0)
{
list[i][length[i]++]=j;
}
}
while(cin>>str)
{
l=str.length();
for (i=0;i<l;i++)
{
if (i==0)
{
for (j=0;j<l;j++)
{
word[j][i].number=word[j][i].value=1;
word[j][i].str=str[j];
}
}
else
{
for (j=0;j<l-i;j++)
{
min=1000;
for (k=0;k<i;k++)
{
if (word[j][k].number==1)
str1=word[j][k].str;
else
{
sprintf(temp,"%d",word[j][k].number);
str2=temp;
str1=str2+"("+word[j][k].str+")";
}
if (word[j+k+1][i-k-1].number==1)
{
str1=str1+word[j+k+1][i-k-1].str;
}
else
{
sprintf(temp,"%d",word[j+k+1][i-k-1].number);
str2=temp;
str1=str1+str2+"("+word[j+k+1][i-k-1].str+")";
}
value=str1.length();
if (value<min)
{
min=value;
word[j][i].number=1;
word[j][i].str=str1;
word[j][i].value=value;
}
}
for (k=0;k<length[i+1];k++)
{
x=list[i+1][k];
y=(i+1)/x;
for (p=0;p<x;p++)
for (o=1;o<y;o++)
{
if (str[j+p]!=str[j+o*x+p])
goto next;
}
sprintf(temp,"%d",y);
str2=temp;
value=word[j][x-1].value+str2.length()+2;
if (value<min)
{
min=value;
word[j][i].number=y;
word[j][i].str=word[j][x-1].str;
word[j][i].value=value;
}
next: ;
}
}
}
}
if (word[0][l-1].number==1)
{
cout<<word[0][l-1].str<<endl;
}
else
{
cout<<word[0][l-1].number<<"("<<word[0][l-1].str<<")"<<endl;
}
}
return 0;
}
谢谢了!!帮忙解读下!!