2.5 Look and Say(看 和 说)

论坛 期权论坛 脚本     
匿名技术用户   2020-12-28 04:43   11   0

2.5 Look and Say

2.5.1 时空限制

Time limit: 1 Seconds Memory limit: 32768K

2.5.2 题目内容

The look and say sequence is defined as follows. Start with any string of digits as the first element in the sequence. Each subsequent element is defined from the previous one by “verbally” describing the previous element. For example, the string 122344111 can be described as “one 1, two 2’s, one 3, two 4’s, three 1’s”. Therefore, the element that comes after 122344111 in the sequence is 1122132431. Similarly, the string 101 comes after 1111111111. Notice that it is generally not possible to uniquely identify the previous element of a particular element. For example, a string of 112213243 1’s also yields 1122132431 as the next element.

Input

The input consists of a number of cases. The first line gives the number of cases to follow. Each case consists of a line of up to 1000 digits.

Output

For each test case, print the string that follows the given string.

Sample Input

3
122344111
1111111111
12345

Sample Output

1122132431
101
1112131415

2.5.3 题目来源

The 2007 ACM Rocky Mountain Programming Contest

2.5.4 汉语翻译

1.题目

看 和 说

看和说系列定义如下:以数字字符串作为这个系列中的第一个元素。每个随后的元素是对它前面一个数字的口头描述。比如,字符串 122344111 可以被描述成“1 个 1,2 个 2,1 个 3,2 个 4,3 个 1”。因此,122344111 应该写成 1122132431。类似地,字符串 1111111111可以写成 101.

2.输入描述

输入中包含多个测试案例。第一行是测试案例的个数。每个测试案例多达 1000 位。

3.输出描述

对于每个测试案例,打印按题意要求的字符串。

4.输入样例

3
122344111
1111111111
12345

5.输出样例

1122132431
101
1112131415

2.5.5 解题思路

和上一题差不多

本题是处理重复子串的问题。虽然输入的都是数字,但应当把它们当成字符串来处理。
由于本题时限一秒,每个字符串的长度多达 1000 位,所以,不好的算法容易超时。
scanf 和 printf 所用的时间大大少于 cin 和 cout 所消耗的时间。由于本题需要频繁输出,采用 cout 则会超过一秒;而使用 printf 则不会超过一秒。这点是 ACM 竞赛中节约时间的常识。一般地,由于 cin 和 cout 调试很方便,所以调试期间使用它们,但是提交判题系统后,如果发现超时,可尝试将 cin 和 cout 改为 scanf 和 printf,看看是不是由于输入输出过于频繁而导致的。

2.5.6 参考答案

#include <iostream> 
#include <string> 
using namespace std; 
int main(int argc, char* argv[]) 
{ 
    string s,t; 
    int n; 
    cin>>n; 
    for(int i=0;i<n;i++) 
 { 
  cin>>s; 
        int c=0; 
        t=s[0]; 
        int temp=0; 
        for(int j=0;j<s.size();j++) 
  {
   if(s[j]==t[0]) 
   {
    temp++; 
     //如果已是最后一个,直接输出
    if(j==s.size()-1) 
    { 
      //cout<<temp<<t[0]; 
     printf("%d%c",temp,t[0]); 
    } 
   } 
   else 
   { 
     //cout<<temp<<t[0]; 
    printf("%d%c",temp,t[0]); 
    t[0]=s[j]; 
    temp=1; 
     //如果已是最后一个,直接输出
    if(j==s.size()-1) 
    { 
      //cout<<temp<<t[0]; 
     printf("%d%c",temp,t[0]); 
    } 
   } 
  } 
  cout<<endl; 
 } 
 return 0; 
} 



分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP