Summer day 10

上午,经过无数次调试,解决无数个智障bug,终于AC一个二维数组旋转的题。是时候回顾下输入输出了。

Uva 10855 Rotated square

题意:将第二个小矩阵旋转,寻找大矩阵中是否包含该pattern,输出包含个数。
Sample Input
4 2
ABBA
ABBB
BAAA
BABB
AB
BB
6 2
ABCDCD
BCDCBD
BACDDC
DCBDCA
DCBABD
ABCDBA
BC
CD
0 0
Sample Output
0 1 0 0
1 0 1 0
思路:match()过程直接暴力,rotate()函数也很简单,只需要推出旋转公式即可。

 temp[j][n-i-1] = obj[i][j]

旋转公式怎么推?以坐标旋转平移的思路可以得到
(i, j) to (j, -i), based on the (0, 0), rotating the array clockwisely for 90 degree.
(j, -i) to (j, n-1-i), based on the (0, 0), move the j+ direction.

#include<cstdio>

char sq[105][105], obj[105][105];
int ns, no;

void match()
{   int flag, cnt = 0;
    for(int i = 0; i+no<=ns; i++) for(int j = 0; j+no<=ns; j++)
    {   if(sq[i][j] == obj[0][0])
        {   for(int x = 0; x<no; x++) for(int y = 0; y<no; y++)
            {   if(sq[i+x][j+y] != obj[x][y]) goto bkpt;
                else if(y == no-1 && x == no-1) cnt++;
            }
        }
        bkpt:;
    }
    printf("%d", cnt);
    return;
}

void rotate() 
{   char temp[105][105];
    for(int i = 0; i<no; i++) for(int j = 0; j<no; j++)
        temp[j][no-i-1] = obj[i][j];
    for(int i = 0; i<no; i++) 
        for(int j = 0; j<no; j++)
            obj[i][j] = temp[i][j];
}

int main()
{
    while(~scanf("%d%d",&ns, &no))
    {   
        if(ns+no == 0) continue;
        getchar();
        for(int i = 0; i<ns; i++)
        {scanf("%s", sq[i]);}getchar();
        for(int i = 0; i<no; i++)
        {scanf("%s", obj[i]);}

        match();printf(" ");
        rotate(); match();printf(" ");
        rotate(); match();printf(" ");
        rotate(); match();printf("\n");
    }
    return 0;
}

Uva 146 ID Codes

题意:一列ID由字典序string组成,求下一个ID。
Sample input
abaacb
cbbaa
#
Sample output
ababac
No Successor
练习next_permulation()。

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int main() {
    string s;
    while (cin >> s && s != "#") {
        if (next_permutation(s.begin(), s.end()))
            cout << s << endl;
        else
            cout << "No Successor" << endl;
    }
    return 0;
}

Uva 11988 悲剧文本

There are several test cases. Each test case is a single line containing at least one and at most 100,000 letters, underscores and two special characters ‘[’ and ‘]’. ‘[’ means the “Home” key is pressed internally, and ‘]’ means the “End” key is pressed internally. The input is terminated by end-of-file (EOF).
Output
For each case, print the Beiju text on the screen.
Sample Input
This_is_a_[Beiju]_text [[]][][]Happy_Birthday_to_Tsinghua_University
Sample Output
BeijuThis_is_a__text Happy_Birthday_to_Tsinghua_University
紫书上都有的经典链表题,然而维护起来好凌乱,WA在于[]位于字符串中间时的情况。无力回天。WA献上。

//WA
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 100010;

int main()
{
    char txt[maxn];
    while(~scanf("%s",txt))
    {   int len = strlen(txt);
        for(int i = len; i>0; i--)
            txt[i] = txt[i-1];
        int list[maxn];
        memset(list, -1, sizeof(list));
        list[0] = 1;
        int bk, temp;
        for(int i = 0; i<len; i++)
        {   if(txt[i+1]!='[' && txt[i+1]!=']' && list[i] == -1)
                list[i] = i+1;
            if(txt[i+1] == '[')
            {   temp = list[0];
                list[0] = i+1;
                bk = i;
            }
            if(txt[i+1] == ']')
            {   list[i] = i+1;
                list[i+1] = temp;
                list[bk] = i+2;
            }
        }
        for(int i = 0; ; i = list[i])
        {
            if(txt[list[i]]!='[' && txt[list[i]] != ']')
                printf("%c", txt[list[i]]);
            if(list[i] == -1)
                break;
        }
        printf("\n");
    }
    return 0;
}

以及正统代码

#include<cstdio>
#include<cstring>
const int maxn = 10000 + 5;
int last, cur, next[maxn];
char s[maxn];

int main()
{
        while(scanf("%s", s+1) == 1)
        {
                int n = strlen(s+1);
                last = cur = 0;
                next[0]=0;

                for(int i = 1; i <= n; i++)
                {
                        char ch = s[i];
                        if(ch == '[')
                                cur = 0;
                        else if(ch == ']')
                                cur = last;
                        else
                        {
                                next[i] = next[cur];
                        next[cur] = i;
                        if(cur == last)
                                last = i;
                        cur = i;
                        }
                }
                for(int i = next[0]; i != 0; i=next[i])
                        printf("%c",s[i]);
                printf("\ns\n");
                for(int i = 0; i<=n;i++)
                        printf("%2d ",i);
                printf("\n");
                for(int i = 0; i<=n; i++)
                        printf("%2c ",s[i]);
                printf("\next\n");
                for(int i = 0; i<=n;i++)
                        printf("%2d ",i);
                printf("\n");
                for(int i = 0; i<=n; i++)
                        printf("%2d ",next[i]);
        }
        return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值