Buggy ICPC(思维+打表)

A - Buggy ICPC
Alan Curing is a famous sports programmer. He is the creator of the theoretical model of computation
known as the Alan Curing Machine (ACM). He’s most famous for creating his own computer for programming competitions: the Integrated Computer for Programming Contests (ICPC). This computer
has a specialized operating system with commands for submitting code and testing executables on sample inputs, an input generator, a wide display for debugging, and a very soft keyboard. However, as it
happens even to the best, Alan’s creation has a nasty bug. Every time Alan types a vowel on the ICPC,
the content of the current line is reversed.
The bug has been extremely hard to track down, so Alan has decided to accept the challenge and
use the computer as it is. He is currently training touch typing on the ICPC. For now, he is only typing
strings using lowercase letters, and no spaces. When Alan types a consonant, it is appended to the end
of the current line, as one would expect. When he types a vowel, however, the typed character is first
added to the end of the line, but right after that the whole line is reversed. For example, if the current
line has “imc” and Alan types “a” (a vowel), for a brief moment the line will become “imca”, but then
the bug kicks in and turns the line into “acmi”. If after that he types the consonants “c”, “p” and “c”,
in that order, the line becomes “acmicpc”.
When practicing, Alan first thinks of the text he wants to type, and then tries to come up with a
sequence of characters he can type in order to obtain that text. He is having trouble, however, since he
realized that he cannot obtain some texts at all (such as “ca”), and there are multiple ways of obtaining
other texts (as “ac”, which is obtained whether he types “ac” or “ca”). Help Alan in his training by
telling him in how many ways he can type each text he wishes to type. A way of typing a text T can
be encoded by a string W with |T| characters such that if the characters are typed on the ICPC in the
order they appear in W (i.e. W1, W2, . . . , W|T|) the final result is equal to T, considering ICPC’s known
bug. Two ways are considered different if they are encoded by different strings. The letters that trigger
the bug in the ICPC when typed are “a”, “e”, “i”, “o” and “u”.
Input
The input consists of a single line that contains a non-empty string T of at most 105
lowercase
letters, representing the text Alan wants to type on the ICPC.
Output
Output a single line with an integer representing the number of distinct ways Alan can type the
desired text T considering ICPC’s known bug.
Sample input 1
ac
Sample output 1
2
Sample input 2
ca
Sample output 2
0
Sample input 3
acmicpc
Sample output 3
3
题意:有一个字符串当你键入元音(a.e.i.o.u)时,字符串会在输入这个元音后翻转。比如你输入eca,字符串会变成ace。现在在给你一个字符串s,让你求有多少种输入的方式,可以使最后的字符串变为s。
解题思路:打表找规律、分类讨论。
显而易见,设cnt为s中元音数目那么

  • cnt=0时。此时way=1
  • cnt=1时。如果s的首位不是元音的话不成立,way=0。s首位是元音的话,相当于把首位的这个元音插入到由其他辅音构成的字符串中。此时way=strlen(len)
  • cnt>1时。这是比较难想到的情况了。寻找规律(打表)可以发现,除了中间的两个元音会对way的数量有影响,其他的都没有影响。way=中间两个元音之间的辅音数量+1.

错因:最后要截至的时候太紧张了,没有讨论元音数目的奇偶性对中间两个元音位置的影响


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define LL long long
#define MT(a,b) memset(a,b,sizeof(a))
const int maxn=2E5+5;
const int ONF=-0x3f3f3f3f;
const int INF=0x3f3f3f3f;
char str[maxn];
int pos[maxn];
int main (){
    scanf("%s",str);
    int ans=0;
    int cnt=0;
    int len=strlen(str);
    for (int i=0;i<len;i++){
        if (str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u')
            pos[++cnt]=i;
    }
    if (cnt==0) ans=1;
    if (cnt!=0){
        if (str[0]!='a'&&str[0]!='e'&&str[0]!='i'&&str[0]!='o'&&str[0]!='u')
            ans=0;
        else{
            if (cnt==1) ans=len;
            else{
                if (cnt&1) ans=pos[cnt/2+2]-pos[cnt/2+1];
                else ans=pos[cnt/2+1]-pos[cnt/2];
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}
发布了33 篇原创文章 · 获赞 15 · 访问量 895

猜你喜欢

转载自blog.csdn.net/weixin_43925900/article/details/103948937