《算法竞赛入门经典》(刘汝佳)5.1

版权声明:沉迷代码,难以自拔 https://blog.csdn.net/qq_33846054/article/details/53378191

5.1.1
WERTYU
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 31 Accepted Submission(s) : 4

http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?cid=3681&pid=1020&ojid=1
Problem Description

A common typing error is to place the hands on the keyboard one row to the right of the correct position. So “Q” is typed as “W” and “J” is typed as “K” and so on. You are to decode a message typed in this manner.

Input
Input consists of several lines of text. Each line may contain digits, spaces, upper case letters (except Q, A, Z), or punctuation shown above [except back-quote (`)]. Keys labelled with words [Tab, BackSp, Control, etc.] are not represented in the input.

Output
You are to replace each letter or punctuation symbol by the one immediately to its left on the QWERTY keyboard shown above. Spaces in the input should be echoed in the output.

Sample Input
O S, GOMR YPFSU/

Sample Output
I AM FINE TODAY.


 1. #include <iostream>
    
    using namespace std;
    
    int main() {   char
    *s="`1234567890-=qwertyuiop[]asdfghjkl;'\zxcvbnm,./";//建立一个字符串常量   int i;   char c;   while ((c=getchar())!=EOF)   {
          for(i=1;s[i]&&s[i]!=c;i++);//'\0'的ASCI码为零,只需循环到C即可
          if (s[i])//如果没有循环到字符串末尾
            putchar(s[i-1]);
          else//其他字符原样输出
            putchar(c);   } }

只是有一件让我很郁闷的事情,明明是按照《算法竞赛入门经典》写的,为什么杭电OJAC不了!?out of time …我自己用麻烦的办法写了一种,但是没有书上的提前结束循环的是s[i]为‘\0’时ASCI码值为零设置地巧妙。

5.1.2
TEX Quotes
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 10299 Accepted: 5364
Description

TEX is a typesetting language developed by Donald Knuth. It takes source text together with a few typesetting instructions and produces, one hopes, a beautiful document. Beautiful documents use double-left-quote and double-right-quote to delimit quotations, rather than the mundane " which is what is provided by most keyboards. Keyboards typically do not have an oriented double-quote, but they do have a left-single-quote and a right-single-quote '. Check your keyboard now to locate the left-single-quote key (sometimes called the “backquote key”) and the right-single-quote key ’ (sometimes called the “apostrophe” or just “quote”). Be careful not to confuse the left-single-quote ` with the “backslash” key . TEX lets the user type two left-single-quotes `` to create a left-double-quote and two right-single-quotes ‘’ to create a right-double-quote. Most typists, however, are accustomed to delimiting their quotations with the un-oriented double-quote ".

If the source contained
“To be or not to be,” quoth the bard, “that is the question.”
then the typeset document produced by TEX would not contain the desired form: “To be or not to be,” quoth the bard, “that is the question.” In order to produce the desired form, the source file must contain the sequence:
To be or not to be,'' quoth the bard,that is the question.’’
You are to write a program which converts text containing double-quote (") characters into text that is identical except that double-quotes have been replaced by the two-character sequences required by TEX for delimiting quotations with oriented double-quotes. The double-quote (") characters should be replaced appropriately by either if the " opens a quotation and by '' if the " closes a quotation. Notice that the question of nested quotations does not arise: The first " must be replaced by, the next by ‘’, the next by , the next by '', the next by, the next by ‘’, and so on.
Input

Input will consist of several lines of text containing an even number of double-quote (") characters. Input is ended with an end-of-file character.
Output

The text must be output exactly as it was input except that:

the first " in each pair is replaced by two ` characters: `` and
the second " in each pair is replaced by two ’ characters: ‘’.
Sample Input

“To be or not to be,” quoth the Bard, “that
is the question”.
The programming contestant replied: “I must disagree.
To C' or not toC’, that is The Question!”
Sample Output

To be or not to be,'' quoth the Bard,that
is the question’’.
The programming contestant replied: ``I must disagree.
To C' or not toC’, that is The Question!’’

http://poj.org/problem?id=1488

#include <iostream>
#include <iomanip>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
//
using namespace std;

int main()
{
  char c;
  int k=1;
  while ((c=getchar())!=-1)//每个字符单独输入
  {
     if (c=='"')
     {
         printf("%c",k?'``':'"');
         //利用条件判断语句,1时输左边,0时输右边,每次K翻转
         k=!k;
     }

     else
        cout<<c;
  }
}

5.1.3
周期串
如果一个字符串可以由某个长度为k的字符串重复多次得到,则该串以k为周期。例如,abcabcabcabc以3为周期(注意,它也以6和12为周期)。输入一个长度不超过80的串,输出它的最小周期。

样例输入:HoHoHo
样例输出:2

#include <iostream>
#include <iomanip>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
//
using namespace std;

int main()
{
  char s[1000];
  int i,j,flag;
  while (cin>>s)
  {
      for (i=1;i<=strlen(s);i++)
      {
          //I代表周期的所有可能值,这里要遍历所有可能周期值
          flag=1;
          for (j=i;j<strlen(s);j++)
          {
              if (s[j]!=s[j%i])
              {
                  flag=0;
                  break;
                  //从第二个潜在周期开始寻找,如果对应位置不等旗帜变为0
              }

          }
          if (flag)
              {
                  cout<<i<<endl;
                  break;
              }
      }
  }
}

我今天好忙啊,可是一旦开始了就不能放弃时间是挤出来的,努力一定不会白费,每天都比前一天快一点点,好一点点。加油

猜你喜欢

转载自blog.csdn.net/qq_33846054/article/details/53378191
今日推荐