HDU2091(预处理+格式控制)

来源:互联网 发布:群发短信软件电脑版 编辑:程序博客网 时间:2024/05/22 01:43

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2091


解题思路:

预处理存储空格,然后手动算出g[ 0 ][ (n * 2 - 1 ) / 2  ] = ch,接下来根据规律对每行从左往右扫一遍,从右往左扫一遍。

最坑的莫过于输出格式,每行不能有空格,这点对于预处理做的有点麻烦。每行要先从后往前扫,找到最后一个ch的位置,这样才能保证每行输出后面没有多余空格。


完整代码:

#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <sstream>#include <iomanip>#include <numeric>#include <cstring>#include <climits>#include <cassert>#include <complex>#include <cstdio>#include <string>#include <vector>#include <bitset>#include <queue>#include <stack>#include <cmath>#include <ctime>#include <list>#include <set>#include <map>using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")typedef long long LL;typedef double DB;typedef unsigned uint;typedef unsigned long long uLL;/** Constant List .. **/ //{const int MOD = int(1e9)+7;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f3f3f3f3f3f3fLL;const DB EPS = 1e-9;const DB OO = 1e20;const DB PI = acos(-1.0); //M_PI;const int maxn = 1111;char g[1111][1111];void init(){    for(int i = 0 ; i < 1111 ; i ++)    {        for(int j = 0 ; j < 1111 ; j ++)            g[i][j] = ' ';    }}void solve(char ch , int n){    init();    int k = (2*n-1)/2;        g[0][k] = ch;    for(int i = 1 ; i < n ; i ++ )    {        if(i == n - 1)        {            for(int j = 0 ; j < 2 * n - 1 ; j ++)            {                g[i][j] = ch;            }        }        else        {            for(int j = 0 ; j <= k ; j ++)            {                if(g[i-1][j] == ch)                {                    g[i][j-1] = ch;                    break;                }            }            for(int j = 2 * n - 2 ; j >= k ; j --)            {                if(g[i-1][j] == ch)                {                    g[i][j+1] = ch;                    break;                }            }        }    }    for(int i = 0 ; i < n ; i ++)    {        int key;        for(int j = 2 * n - 2 ; j >= 0 ; j --)            if(g[i][j] == ch)            {                key = j;                break;            }        for(int j = 0 ; j <= key ; j ++)        {            cout << g[i][j];        }        cout << endl;    }}int main(){    #ifdef DoubleQ    freopen("in.txt","r",stdin);    #endif    std::ios::sync_with_stdio(false);    std::cin.tie(0);    int n;    char ch;    int flag = 0;    while(cin >> ch)    {        if(ch == '@')            break;        cin >> n;        if(flag)        {            cout << "\n";        }        flag = 1;        solve(ch , n);    }}

 

0 0