UVA11661 Burger Time?【文本处理】

Everybody knows that along the more important highways there are countless fast food restaurants. One can find easily hamburgers, hot dogs, pizzas, sandwiches … food everywhere.
    Many times the problem isn’t to find a restaurant but a drugstore. After a big lunch with fast food it’s normal that we need a drugstore because our stomach begins to feel pain.
    Given the locations of the restaurants and drugstores on a highway, you want to determine the minimum distance between a restaurant and a drugstore.
Input
The first line of each test case gives an integer L (1 ≤ L ≤ 2000000) indicating the length of the highway.
    The second line of each test case contains a string S of length L, showing the positions of the restaurants and drugstores along the highway in the following manner:
• The character ‘R’ represents a place with a restaurant.
• The character ‘D’ represents a place with a drugstore.
• The character ‘Z’ represents a place with a restaurant and a drugstore.
• The character ‘.’ represents an empty place.
    You can suppose that every test case has at least one restaurant and at least one drugstore. The end of the input is indicated when L = 0.
Output
For each case in the input, print one line with the minimum distance between a restaurant and a drugstore.
Sample Input
2
RD
5
…Z…
10
.R…D.
10
.R…Z…D.
10
…D…R…
25
…D…R.RR…DD…D.R…R
0
Sample Output
1
0
7
0
3
2

问题链接UVA11661 Burger Time?
问题简述:给定一个长度n和字符串字符串,计算字符串中的R和D之间的最小距离,如果其中有Z输出0。
问题分析
    简单题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA11661 Burger Time? */

#include <bits/stdc++.h>

using namespace std;

const int N = 2000000;
char s[N + 1];

int main()
{
    int n;
    while(~scanf("%d", &n) && n) {
        scanf("%s", s);

        int minm = N;
        for(int i = 0, j = 0; s[i]; i++)
            if(s[i] == 'Z') {
                minm = 0;
                break;
            } else if(s[i] != '.') {
                if(s[i] != s[j] && s[j] != '.')
                    minm = min(minm, i - j);
                j = i;
            }

        printf("%d\n", minm);
    }

    return 0;
}
发布了2126 篇原创文章 · 获赞 2306 · 访问量 254万+

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/104541979
今日推荐