今天的網絡比以前正常了一點。總算可以到PKU上做題目了!PKU2896“Changing Phone Numbers”

        這道題我認爲是一道模擬題,沒有什麼特別的算法,但是代碼很長。不是數學問題,而是屬於剪枝和數據結構的問題。由於一開始偷懶。用了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;
}

發佈了43 篇原創文章 · 獲贊 0 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章