ACM_一道耗时间的水题

Posted 霜雪千年

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ACM_一道耗时间的水题相关的知识,希望对你有一定的参考价值。

一道耗时间的水题

Time Limit: 2000/1000ms (Java/Others)

Problem Description:

Do you know how to read the phone numbers in English? Now let me tell you.
For example, In China, the phone numbers are 11 digits, like: 15012233444. Someone divides the numbers into 3-4-4 format, i.e. 150 1223 3444. While someone divides the numbers into 3-3-5 format, i.e. 150 122 33444. Different formats lead to different ways to read these numbers:
150 1223 3444 reads one five zero one double two three three triple four.
150 122 33444 reads one five zero one double two double three triple four.
Here comes the problem:
Given a list of phone numbers and the dividing formats, output the right ways to read these numbers.
Rules:
Single numbers just read them separately.
2 successive numbers use double.
3 successive numbers use triple.
4 successive numbers use quadruple.
5 successive numbers use quintuple.
6 successive numbers use sextuple.
7 successive numbers use septuple.
8 successive numbers use octuple.
9 successive numbers use nonuple.
10 successive numbers use decuple.
More than 10 successive numbers read them all separately.

Input:

The first line of the input gives the number of test cases, T(1 ≤ T ≤ 100).
T test cases follow. Each line contains a phone number N(1 ≤ length of N ≤ 100) and the dividing format F, one or more positive integers separated by dashes (-), without zeros and whose sum always equals the number of digits in the phone number.

Output:

For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the reading sentence in English whose words are separated by a space.

Sample Input:

3
15012233444 3-4-4
15012233444 3-3-5
12223 2-3

Sample Output:

Case #1: one five zero one double two three three triple four
Case #2: one five zero one double two double three triple four
Case #3: one two double two three
解题思路:这道题的题意比较简单,处理起来比较麻烦,但掌握了这个知识点(C++的获取子串的函数substr()),就简单多了:basic_string substr( size_type index, size_type num = npos );substr()返回本字符串的一个子串,从index开始,长num个字符。如果没有指定,将是默认值 string::npos。这样,substr()函数将简单的返回从index开始的剩余的字符串。对应每个子串中某个字符出现的次数n如果为1时,则只输出该字符对应的英语单词;如果大于10,则输出n个该字符对应的英语单词;否则(n>1&&n<=10)输出n对应倍数单词和该字符对应的英语单词。
AC代码:
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 105;//因为字符串s2中可能会出现100个‘1‘,所以这里取105
 4 const string str1[]={"decuple","","double","triple","quadruple","quintuple","sextuple","septuple","octuple","nonuple"};
 5 const string str2[]={"zero","one","two","three","four","five","six","seven","eight","nine"};
 6 int t,n,k,x,s[maxn],e[maxn];string s1,s2,tp[maxn];//s数组记录截取字符串s1的起始位置,e数组记录截取字符的个数
 7 int main(){
 8     cin>>t;getchar();//吃掉回车符对字符串的影响
 9     for(int i=1;i<=t;++i){
10         cin>>s1>>s2;s[0]=k=x=0;//s[0]起始位置为0,k记录分成子串的个数
11         cout<<"Case #"<<i<<":";
12         for(unsigned int j=0;j<=s2.length();++j){
13             if(s2[j]==-||s2[j]==\0){e[k++]=x;x=0;}
14             else x=x*10+s2[j]-0;
15         }
16         for(int j=0;j<k;++j)s[j+1]=s[j]+e[j];//累加起始位置
17         for(int j=0;j<k;++j)tp[j]=s1.substr(s[j],e[j]);//获得对应子串
18         for(int j=0;j<k;++j){n=1;//n记录相同元素的个数,初始值为1
19             for(unsigned int h=0;h<tp[j].length();++h){
20                 if(tp[j][h]!=tp[j][h+1]){
21                     if(1<n&&n<=10)cout<<" "+str1[n%10];//取余操作
22                     if(n<=10)cout<<" "+str2[tp[j][h]-0];
23                     else{//n大于10,则输出n个对应的字符串
24                         for(int m=1;m<=n;++m)
25                             cout<<" "+str2[tp[j][h]-0];
26                     }n=1;//n重置为1
27                 }
28                 else n++;//相同个数加1
29             }
30         }
31         cout<<endl;
32     }
33     return 0;
34 }

以上是关于ACM_一道耗时间的水题的主要内容,如果未能解决你的问题,请参考以下文章

碰到日期题就怕的我来写一道水题吧

水果,一道对结构体排序的水题

字符串期间的水题柱

hdu-3790最短路刷题

ACM_输出格式(水题)

HDU2193-AVL-数据结构-AVL