《算法竞赛入门经典》(刘汝佳)——字符串(基础)

1:WERTYU

poj 2538 WERTYU

#include<stdio.h>
int main()
{
    char c;
    char s[]="`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";
    while((c=getchar())!=EOF)
    {
        int i;
        for(i=0;s[i]&&s[i]!=c;i++);
        if(s[i])
            printf("%c",s[i-1]);
        else 
            printf("%c",c);
    }
    return 0;
}
View Code

 

2:TeX括号

 poj 1488 TEX Quotes

#include<stdio.h>
int main()
{
    char c;
    int k=1;
    while((c=getchar())!=EOF)
    {
        if(c=='"'){
            if(k>0)printf("``");
            else printf("''");
            k=-k;
        }
        else
            printf("%c",c);
    }
    return 0;
}
View Code

 

3:周期串

poj 2406 Power Strings

#include<stdio.h>
#include<string.h>
char s[1000005];
int main()
{

    while(scanf("%s",s)!=EOF)
    {
        if(strcmp(s,".")==0)break;
        int len=strlen(s);
        for(int i=0;i<len;i++)
        {
            if(len%(i+1)==0)
            {
                int flag=0;
                for(int j=i;j<len;j++)
                {
                    if(s[j]!=s[j%(i+1)])
                    {
                        flag=1;
                        break;
                    }
                }
                if(flag==0)
                {
                    printf("%d\n",len/(i+1));
                    break;
                }
            }
        }
    }
    return 0;
}
View Code

 

 

posted @ 2014-03-06 17:59  laiba2004  Views(218)  Comments(0Edit  收藏  举报