sdut 第三屆ACM/ICPC程式設計知識競賽網路賽 字串拓展

來源:互聯網
上載者:User

字串擴充


Time Limit: 1000MS Memory limit: 65536K

題目描述

Tom有些時候為了記錄的方便,常常將一些連續的字元用擴充符'-'簡單表示。比如abcdefg可以簡寫為a-g,即用起始的字元和終止字元中間加上一個擴充符'-'來表示這個字串。但是為了處理的方便,Tom又必須將這些我們簡單記法擴充成原來的字串。很明顯要是人工來做的話必定很麻煩,Tom知道電腦可以協助他完成這個任務,但是他卻不會編程,這的確讓他很上火。他知道今天是山東理工大學第三屆ACM校賽的日子,屆時來自全校的編程愛好者都會來參加比賽,他很興奮,因為這個困惑他良久的問題終於要被解決了。給你一個含有擴充符'-'的字串,你的任務就是將他還原成原來的字串。要求是只處理[a-z]、[A-Z]、[0-9]範圍內的字元擴充,即只有當擴充符前後的字元同時是小寫字母、大寫字母或數字時並且擴充符前面的字元不大於後面的字元才進行擴充,其它情況不進行擴充,原樣輸出。例如:a-R、D-e、0-b、4-B等字串都不進行擴充。

輸入

第一行是一個正整數T,表示共有T組測試資料(T < 100)。下面的T行,每一行包括一個長度不大於1000的待擴充字元串.

輸出

每組測試資料輸出一行擴充後的字串。

樣本輸入
3ADEa-g-m02acm-0-5-a-ac-cm-m-A-AC-CM-MWelcometothe3rdACM/ICPCCampusProgrammingContestofSDUT-1-3-A-z-a-Z
樣本輸出
ADEabcdefghijklm02acm-012345-aaccmm-AACCMMWelcometothe3rdACM/ICPCCampusProgrammingContestofSDUT-123-A-z-a-Z
提示 本題思路:先輸入一串字串,判斷'-'兩端的字元是否符合[a-z]、[A-Z]、[0-9]範圍內,如果符合則刪掉‘-’
,並按照兩端字元的ascii碼輸入兩字元中間的字元,不符合條件直接輸出。
#include <stdio.h>#include <string.h>char str[30000];int main(){    int num,i;    scanf("%d",&num);    scanf("%*c");    for(i = 1;i <= num;i++)    {        int len,j,m1 = 0,m2 = 0,m;        scanf("%s",str);        len = strlen(str);        for(j = 0;j < len;j++)         {              if(j == 0 || j == len-1 )              {                  printf("%c",str[j]);              }else               {                    if(str[j] == '-'&&str[j-1] >= '0'&&str[j+1] <='9'&&str[j+1] >= str[j-1])                {                    m1 = str[j-1] + 1;                    m2 = str[j+1] - 1;   //判斷字元ascii                    for(m = m1;m <= m2;m++)                        printf("%c",m);                        m1 = 0;                                m2 = 0;                }else if(str[j-1] >= 'A'&&str[j]=='-'&&str[j+1] <= 'Z'&&str[j+1] >= str[j-1])                {                     m1 = str[j-1] + 1;                    m2 = str[j+1] - 1;                    for(m = m1;m <= m2;m++)                        printf("%c",m);                        m1 = 0;                        m2 = 0;                }else if(str[j-1] >= 'a'&&str[j]=='-'&&str[j+1] <= 'z'&&str[j+1] >= str[j-1])                {                     m1 = str[j-1] + 1;                    m2 = str[j+1] - 1;                    for(m = m1;m <= m2;m++)                        printf("%c",m);                        m1 = 0;                        m2 = 0;                }else                {                   printf("%c",str[j]);                }               }    }    printf("\n");    }    return 0;}/*#include<stdio.h>#include<string.h>int main(){int  n;scanf("%d",&n);char st[1001];int k;int i;  int j;int t;scanf("%*c");while(n--){  scanf("%s",st);  k=strlen(st);  for(i=0;i<k;i++)  {      if(i==0||i==k-1)printf("%c",st[i]);      else if(st[i]=='-'&&(st[i-1]>='0'&&st[i+1]<='9'||st[i-1]>='a'&&st[i+1]<='z'||st[i-1]>='A'&&st[i+1]<='Z')&&st[i-1]<=st[i+1])      {         t=st[i+1]-st[i-1];         for(j=0;j<t-1;j++)         {             st[i-1]+=1;            printf("%c",st[i-1]);         }      }      else printf("%c",st[i]);  }  printf("\n");}return 0;}*/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.