HOJ 3188 - Duang~(字符串)

又来一道字符串
传送门:http://acm.hit.edu.cn/hojx/showproblem/3188/
3188 - Duang~

Time limit : 2 s Memory limit : 400 mb
Submitted : 78 Accepted : 15
Submit

Problem Description
Bus Xiao and Smile Dog watch bilibili everyday. One day they find a video which title is Cheng Long’s shampoo.after they watch the video, Smile Dog give many text and ask Bus Xiao to change the text into the way he want by following rule:
1. If you find “chenglong” in his text ,you should change it to “wojiateji”, example : chenglong —> wojiateji
2. If you find “toufa” in his text ,you should change it to “duang”, example : toufa —> duang
3. The new word should follow original word’s case. example : the original word is “ChEnglOnG “, the new word is “WoJiatEjI”.
Input

There are multiple test case, input will end with a single number -1.

The first line contain an integer N (1 <= N <= 10^6), representing the text’s length. following line is the text. The text will only contain letter “a-z,A-Z”.
Output

Printf the Case and the new text transform by the rule.
Sample Input
16
ChEngLongdetouFa
28
ShitoUfaDetejishitejidetOufA
-1
Sample Output
Case #1 : WoJiaTejideduaNg
Case #2 : ShiduAngDetejishitejidedUanG

这个题目从中好好复习了一番str的各种函数,strcpy()和strncpy(),还有strcmp();
总结一下:
strcpy(*str1, *str2):是将2的内容复制到1中,都是传指针。所以可以对2定位,然后一直复制到\0。这个函数的缺点是:会越界!!当你不知道1中是否有足够大的容量就这样搞,会死的很难看。
strncpy(*str1, *str2, n):与上面那个不同的是,这个指定了在2中传递的起点和终点,比上面的安全得多。但是传好后要在1中的结尾处加\0,不然也会发生奇怪的事情。
strcmp(*str1, *str2):这个函数其实就是个for循环,它从前往后按字典序(ASCII)比较1和2中的字符。
**返回值是int型:
1代表1 < 2;
0代表相等;
1代表1 > 2;**

很有意思的事情是,这个题原来在老OJ上是4MB,这样的话就要用getchar()一个个扫,特殊情况的话特殊处理。但是仔细一看这个限制400MB……嗯,完全不慌啊,随便乱开数组存东西,最终也就用了7.2MB吧。
下来可以好好想想getchar()的怎么写,感觉也不难,会更有意思一点。
AC代码如下:

#include <stdio.h>
#include <string.h>
#define maxn 1000005
char s[maxn];
char ts[maxn];
char ans[maxn];
char c[10] = "chenglong";
char t[6] = "toufa";
char tmp1[10];
char tmp2[6];
int tn[maxn];
int main()
{
    int n;
    int kase = 0;
    while(scanf("%d", &n) != EOF && n > 0)
    {
        memset(tn, 0, sizeof(tn));
        memset(s, 0, sizeof(s));
        memset(ts, 0, sizeof(ts));
        scanf("%s", s);
        for(int i = 0; i < n; i++)
        {
            if(s[i] <= 'Z' && s[i] >= 'A')
            {
                tn[i] = 1;
                ts[i] = s[i] + 32;
            }
            else
            {
                tn[i] = 0;
                ts[i] = s[i];
            }
        }
        for(int i = 0; i < n; i++)
        {
            if(ts[i] == 'c')
            {
                strncpy(tmp1, ts+i, 9);
                tmp1[9] = '\0';
                if(!strcmp(tmp1, c))
                {
                    ans[i] = 'w';
                    ans[i+1] = 'o';
                    ans[i+2] = 'j';
                    ans[i+3] = 'i';
                    ans[i+4] = 'a';
                    ans[i+5] = 't';
                    ans[i+6] = 'e';
                    ans[i+7] = 'j';
                    ans[i+8] = 'i';
                    for(int j = i; j < i+9; j++)
                    {
                        if(tn[j])
                            ans[j] -= 32;
                    }
                    i += 8;
                }
                else
                {
                    if(tn[i])
                        ans[i] = ts[i] - 32;
                    else
                        ans[i] = ts[i];
                }
            }

            else if(ts[i] == 't')
            {
                strncpy(tmp2, ts+i, 6);
                tmp2[5] = '\0';
                if(!strcmp(tmp2, t))
                {
                    ans[i] = 'd';
                    ans[i+1] = 'u';
                    ans[i+2] = 'a';
                    ans[i+3] = 'n';
                    ans[i+4] = 'g';

                    for(int j = i; j < i+9; j++)
                    {
                        if(tn[j])
                            ans[j] -= 32;
                    }
                    i += 4;
                }
                else
                {
                    if(tn[i])
                        ans[i] = ts[i] - 32;
                    else
                        ans[i] = ts[i];
                }
            }
            else
            {
                if(tn[i])
                    ans[i] = ts[i] - 32;
                else
                    ans[i] = ts[i];
            }
        }
        printf("Case #%d : %s\n", ++kase, ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值