今天的网络比以前正常了一点。总算可以到PKU上做题目了!PKU2896“Changing Phone Numbers”
2006-07-28 16:06
405 查看
这道题我认为是一道模拟题,没有什么特别的算法,但是代码很长。不是数学问题,而是属于剪枝和数据结构的问题。由于一开始偷懒。用了vector,想不到超时。我想改为链表会快很多。毕竟要以空间换取时间。顺便说说今天的网络,好了一些,虽然还是经常掉线,最起码不是昨天的一秒掉一次了。这里的气氛很好,每个同学都在努力的编程。我想,等我们暑假集训完毕后,我们应该写了几万行代码吧。今天早上三系的老师高林给我们讲算法,一个字,牛。开拓了我们的思维,她主要是研究计算机和生物领域的。听她讲课,知道了其实生物计算就是简单算法的复杂应用,虽然道理很简单,但是走的路还很长。
我的程序代码:
/*
Changing Phone Numbers
Time Limit:1000MS Memory Limit:65536K
Total Submit:322 Accepted:45
Description
You are working for OTC, the Olandican Telecommunication Company. Unfortunately, OTC does not decide wisely in assigning telephone numbers to the clients. For example, it did not make a good estimate on demand increase. As a result, it had to increase the number of digits in the local telephone numbers of Oland (the capital) from six to seven, and then again from seven to eight digits.
As usual, a telephone number consists of two parts: an area code, and the local number within that area code. For example, if 021 is the area code of the Oland city, a telephone number in that city may be 0211234567. Note that no area code is the prefix of another area code. The process of changing telephone numbers is not easy though. It requires updates to several databases of millions of records. Fortunately, these changes follow a limited number of rules as follows:
1. For all local numbers in a given area code, repeat the ith digit. For example, for the area code 021, repeating the second digit causes the number 0211234567 change to 02112234567.
2. For all numbers in a given area code, swap the ith and the (i + 1)th digit. For example, for the area code 021, swapping the second and the third digits causes the number 0211234567 change to 0211324567.
3. Change a given area code. For example, changing 021 to 0211 causes the number 0211234567 change to 02111234567.
Note that changing area codes in the third rule preserves the property that no area code is the prefix of another. You are to write a program that given the area code information, and the set of all telephone numbers, plus a given set of rules, determine the resulting telephone numbers after the changes.
Input
The first part of the input describes the set of area codes. It starts with a line containing a single integer A (1 <= A <= 1000), which is the number of area codes in Olandica, followed by A lines of the following form:
area-code area-name
where area-code is a string of at least one, and at most 5 digits, and area-name is a string of at least one, and at most 20 letters (both uppercase and lowercase). There are no two lines with the same area-code or area-name.
The second part of the input describes the rules applied by OTC. The first line of this part contains a single integer R (0 <= R <= 10000), the number of rules applied, followed by R lines of the following form:
year rule-info
where year is the year in which the rule is applied. You may assume the rule is applied on the first day of the year, and at most one rule is applied each year. The rule-info part depends on the specific rule applied. The following list shows the rule-info formats corresponding to the rules described in the problem statement:
? 1 area-name i
? 2 area-name i
? 3 area-name new-area-code
You can assume that input data is consistent; i.e. in the rules section, indices are not out of range, and an area code will never be prefix of another, at any fixed time. The third part of the input consists of several lines of the following form:
year1 year2 number
The query says that in year1, there was a telephone number number, and asks for that number in year2 (year1 <= year2). You must change the number according to the rules applied in the years between year1 + 1 and year2 in that order. The queries end with a line containing three zeros. The years are positive integers less than 10^9.
In every line in the input containing more than one data item (number or name), the data items are separated by one or more spaces. There may be arbitrary number of leading or trailing spaces too. Each data item is at least one character. Each line of the input is at most 50 characters.
Output
The output contains one line corresponding to each query containing the changed number.
Sample Input
4
021 Oland
0511 Moland
0311 Boland
03121 Kamand
7
2002 1 Moland 2
2001 3 Moland 0515
2003 2 Boland 3
2005 1 Kamand 1
2000 1 Kamand 1
1999 1 Kamand 1
1998 3 Oland 012345
2000 2005 0311243426
2000 2005 05113837462
2000 2005 03121437478
0 0 0
Sample Output
0311244326
051538837462
031214437478
Source
Tehran 2005
*/
#include "iostream"
#include "vector"
#include "string"
#include "algorithm"
using namespace std;
typedef struct
{
char areacode[60];
char areaname[21];
}Area1;
typedef struct
{
int year;
int rule;
char areaname[21];
int i;
char areacode[60];
}Area2;
typedef struct
{
int begin;
int end;
char num[500];
}Info;
bool cmp(Area2 c1,Area2 c2)
{
if(c1.year>=c2.year)
return true;
else
return false;
}
int main()
{
vector<Area1> area1;
vector<Area2> area2;
vector<Info> info;
vector<Area2> area21;
Area1 temp1;
Area2 temp2;
Info temp3;
char tempnum[500],tempcode[60];
int n,t,i,k,j,flag;
char ch1,ch2;
cin>>n;
if(n<1||n>1000)
return 0;
for(i=0;i<n;i++)
{
cin>>temp1.areacode>>temp1.areaname;
area1.push_back(temp1);
}
cin>>t;
if(t<1||t>10000)
return 0;
for(i=0;i<t;i++)
{
cin>>temp2.year>>temp2.rule>>temp2.areaname;
if(temp2.rule==3)
cin>>temp2.areacode;
else
cin>>temp2.i;
area2.push_back(temp2);
}
while(cin>>temp3.begin>>temp3.end>>temp3.num&&temp3.begin&&temp3.end)
{
info.push_back(temp3);
}
k=0;
while(k<(info.size()))
{
i=0;
flag=-1;
while(i<n)
{
for(j=0;j<(strlen(area1[i].areacode));j++)
{
if(info[k].num[j]!=area1[i].areacode[j])
break;
}
if(j==strlen(area1[i].areacode))
flag=i;
if(flag+1)
break;
i++;
}
for(i=0;i<t;i++)
{
if(!strcmp(area1[flag].areaname,area2[i].areaname))
{
temp2=area2[i];
area21.push_back(temp2);
}
}
j=0;
for(i=0;i<(strlen(info[k].num));i++)
{
if(i<(strlen(area1[flag].areacode)))
{
tempcode[j]=info[k].num[i];
j++;
}
else if(i==strlen(area1[flag].areacode))
{
tempcode[j]='/0';
j=0;
tempnum[j]=info[k].num[i];
j++;
}
else
{
tempnum[j]=info[k].num[i];
j++;
}
}
tempnum[j]='/0';
sort(area21.begin(),area21.end(),cmp);
for(i=info[k].begin;i<=info[k].end;i++)
{
if(i==area21.back().year)
{
if(area21.back().rule==1)
{
for(j=0;j<(strlen(tempnum)+1);j++)
{
if(j>=area21.back().i)
{
ch2=tempnum[j];
tempnum[j]=ch1;
ch1=ch2;
}
else
ch1=tempnum[j];
}
tempnum[j]='/0';
}
if(area21.back().rule==2)
{
for(j=0;j<(strlen(tempnum));j++)
{
if(j==area21.back().i)
{
tempnum[j-1]=tempnum[j];
tempnum[j]=ch1;
break;
}
else
ch1=tempnum[j];
}
}
if(area21.back().rule==3)
{
strcpy(tempcode,area21.back().areacode);
}
area21.pop_back();
if(area21.empty())
break;
}
if(i>area21.back().year)
area21.pop_back();
if(area21.back().year>info[k].end)
area21.pop_back();
}
cout<<tempcode<<tempnum<<endl;
k++;
}
return 0;
}
我的程序代码:
/*
Changing Phone Numbers
Time Limit:1000MS Memory Limit:65536K
Total Submit:322 Accepted:45
Description
You are working for OTC, the Olandican Telecommunication Company. Unfortunately, OTC does not decide wisely in assigning telephone numbers to the clients. For example, it did not make a good estimate on demand increase. As a result, it had to increase the number of digits in the local telephone numbers of Oland (the capital) from six to seven, and then again from seven to eight digits.
As usual, a telephone number consists of two parts: an area code, and the local number within that area code. For example, if 021 is the area code of the Oland city, a telephone number in that city may be 0211234567. Note that no area code is the prefix of another area code. The process of changing telephone numbers is not easy though. It requires updates to several databases of millions of records. Fortunately, these changes follow a limited number of rules as follows:
1. For all local numbers in a given area code, repeat the ith digit. For example, for the area code 021, repeating the second digit causes the number 0211234567 change to 02112234567.
2. For all numbers in a given area code, swap the ith and the (i + 1)th digit. For example, for the area code 021, swapping the second and the third digits causes the number 0211234567 change to 0211324567.
3. Change a given area code. For example, changing 021 to 0211 causes the number 0211234567 change to 02111234567.
Note that changing area codes in the third rule preserves the property that no area code is the prefix of another. You are to write a program that given the area code information, and the set of all telephone numbers, plus a given set of rules, determine the resulting telephone numbers after the changes.
Input
The first part of the input describes the set of area codes. It starts with a line containing a single integer A (1 <= A <= 1000), which is the number of area codes in Olandica, followed by A lines of the following form:
area-code area-name
where area-code is a string of at least one, and at most 5 digits, and area-name is a string of at least one, and at most 20 letters (both uppercase and lowercase). There are no two lines with the same area-code or area-name.
The second part of the input describes the rules applied by OTC. The first line of this part contains a single integer R (0 <= R <= 10000), the number of rules applied, followed by R lines of the following form:
year rule-info
where year is the year in which the rule is applied. You may assume the rule is applied on the first day of the year, and at most one rule is applied each year. The rule-info part depends on the specific rule applied. The following list shows the rule-info formats corresponding to the rules described in the problem statement:
? 1 area-name i
? 2 area-name i
? 3 area-name new-area-code
You can assume that input data is consistent; i.e. in the rules section, indices are not out of range, and an area code will never be prefix of another, at any fixed time. The third part of the input consists of several lines of the following form:
year1 year2 number
The query says that in year1, there was a telephone number number, and asks for that number in year2 (year1 <= year2). You must change the number according to the rules applied in the years between year1 + 1 and year2 in that order. The queries end with a line containing three zeros. The years are positive integers less than 10^9.
In every line in the input containing more than one data item (number or name), the data items are separated by one or more spaces. There may be arbitrary number of leading or trailing spaces too. Each data item is at least one character. Each line of the input is at most 50 characters.
Output
The output contains one line corresponding to each query containing the changed number.
Sample Input
4
021 Oland
0511 Moland
0311 Boland
03121 Kamand
7
2002 1 Moland 2
2001 3 Moland 0515
2003 2 Boland 3
2005 1 Kamand 1
2000 1 Kamand 1
1999 1 Kamand 1
1998 3 Oland 012345
2000 2005 0311243426
2000 2005 05113837462
2000 2005 03121437478
0 0 0
Sample Output
0311244326
051538837462
031214437478
Source
Tehran 2005
*/
#include "iostream"
#include "vector"
#include "string"
#include "algorithm"
using namespace std;
typedef struct
{
char areacode[60];
char areaname[21];
}Area1;
typedef struct
{
int year;
int rule;
char areaname[21];
int i;
char areacode[60];
}Area2;
typedef struct
{
int begin;
int end;
char num[500];
}Info;
bool cmp(Area2 c1,Area2 c2)
{
if(c1.year>=c2.year)
return true;
else
return false;
}
int main()
{
vector<Area1> area1;
vector<Area2> area2;
vector<Info> info;
vector<Area2> area21;
Area1 temp1;
Area2 temp2;
Info temp3;
char tempnum[500],tempcode[60];
int n,t,i,k,j,flag;
char ch1,ch2;
cin>>n;
if(n<1||n>1000)
return 0;
for(i=0;i<n;i++)
{
cin>>temp1.areacode>>temp1.areaname;
area1.push_back(temp1);
}
cin>>t;
if(t<1||t>10000)
return 0;
for(i=0;i<t;i++)
{
cin>>temp2.year>>temp2.rule>>temp2.areaname;
if(temp2.rule==3)
cin>>temp2.areacode;
else
cin>>temp2.i;
area2.push_back(temp2);
}
while(cin>>temp3.begin>>temp3.end>>temp3.num&&temp3.begin&&temp3.end)
{
info.push_back(temp3);
}
k=0;
while(k<(info.size()))
{
i=0;
flag=-1;
while(i<n)
{
for(j=0;j<(strlen(area1[i].areacode));j++)
{
if(info[k].num[j]!=area1[i].areacode[j])
break;
}
if(j==strlen(area1[i].areacode))
flag=i;
if(flag+1)
break;
i++;
}
for(i=0;i<t;i++)
{
if(!strcmp(area1[flag].areaname,area2[i].areaname))
{
temp2=area2[i];
area21.push_back(temp2);
}
}
j=0;
for(i=0;i<(strlen(info[k].num));i++)
{
if(i<(strlen(area1[flag].areacode)))
{
tempcode[j]=info[k].num[i];
j++;
}
else if(i==strlen(area1[flag].areacode))
{
tempcode[j]='/0';
j=0;
tempnum[j]=info[k].num[i];
j++;
}
else
{
tempnum[j]=info[k].num[i];
j++;
}
}
tempnum[j]='/0';
sort(area21.begin(),area21.end(),cmp);
for(i=info[k].begin;i<=info[k].end;i++)
{
if(i==area21.back().year)
{
if(area21.back().rule==1)
{
for(j=0;j<(strlen(tempnum)+1);j++)
{
if(j>=area21.back().i)
{
ch2=tempnum[j];
tempnum[j]=ch1;
ch1=ch2;
}
else
ch1=tempnum[j];
}
tempnum[j]='/0';
}
if(area21.back().rule==2)
{
for(j=0;j<(strlen(tempnum));j++)
{
if(j==area21.back().i)
{
tempnum[j-1]=tempnum[j];
tempnum[j]=ch1;
break;
}
else
ch1=tempnum[j];
}
}
if(area21.back().rule==3)
{
strcpy(tempcode,area21.back().areacode);
}
area21.pop_back();
if(area21.empty())
break;
}
if(i>area21.back().year)
area21.pop_back();
if(area21.back().year>info[k].end)
area21.pop_back();
}
cout<<tempcode<<tempnum<<endl;
k++;
}
return 0;
}
相关文章推荐
- 下载一首网络歌曲 该歌曲地址获取不到歌曲的大小,但可以正常播放
- android httpurlconnection或者httpclient总是超时,服务器正常,但网络正常,浏览器可以上网
- android httpurlconnection或者httpclient总是超时,服务器正常,但网络正常,浏览器可以上网
- MAC电脑出现,QQ可以正常使用,浏览器显示无网络解决办法
- java中可以通过调用ping命令来判断网络是否连接正常
- 今天总算可以开通博客了
- 电信的网络可以在烂一点吗?一天都在断网上线中徘徊。
- Android访问网络(可以正常使用)
- eclipse J2ME 声音不能正常播放(以前可以)
- 产生随机数可以提高网络安全的新方法(New method of producing random numbers could improve cybersecurity)
- 今天网络专业课的一点总结
- 【转】Internet连接正常但是没有网络,禁用以太网以后再重新启动就可以使用了,原因是什么?
- 今天无意中发现JavaBean类基本都要求实现了Serializable接口,以前只是知道序列化以后,可以通过io流的方式将对象序列化和反序列化,进行存取,但不知道为什么需要序列化,今天总结一下
- 今天发现c#中的实例后的对象是不能调用静态方法,以前学Java的时候是可以的.
- 遇没遇到过这种情况:某个软件以前可以正常使用,现在突然启动白屏或重启了!
- hdu 4001今天网络赛的题目。。。
- hdu 4001今天网络赛的题目。。。
- 今天搞了一个下午,总算是把并查集了解了一点
- 我们公司原来C++招聘考试题,题目难度正常,没有稀奇古怪的题,如果答对60分以上,恭喜你基本算一个合格的网络开发工程师了。
- 奇怪问题绑定和监听127.0.0.1把网络禁用还是可以成功。当网络断开时accept不会返回错误。网络再次连上时还能正常工作。