Code obfuscatio (翻译!)

Description

Kostya likes Codeforces contests very much. However, he is very disappointed that his solutions are frequently hacked. That's why he decided to obfuscate (intentionally make less readable) his code before upcoming contest.

To obfuscate the code, Kostya first looks at the first variable name used in his program and replaces all its occurrences with a single symbol a, then he looks at the second variable name that has not been replaced yet, and replaces all its occurrences with b, and so on. Kostya is well-mannered, so he doesn't use any one-letter names before obfuscation. Moreover, there are at most 26 unique identifiers in his programs.

You are given a list of identifiers of some program with removed spaces and line breaks. Check if this program can be a result of Kostya's obfuscation.

Input

In the only line of input there is a string S of lowercase English letters (1 ≤ |S| ≤ 500) — the identifiers of a program with removed whitespace characters.

Output

If this program can be a result of Kostya's obfuscation, print "YES" (without quotes), otherwise print "NO".

Sample Input

Input
abacaba
Output
YES
Input
jinotega
Output
NO

Hint

In the first sample case, one possible list of identifiers would be "number string number character number string number". Here how Kostya would obfuscate the program:

  • replace all occurences of number with a, the result would be "a string a character a string a",
  • replace all occurences of string with b, the result would be "a b a character a b a",
  • replace all occurences of character with c, the result would be "a b a c a b a",
  • all identifiers have been replaced, thus the obfuscation is finished.

题目意思:Kostya尝试使用小写字母来替换变量名,先从a替换第一个变量名以及后面相同的变量一次类推,问最后所给的字符串是否是一个合法的字符串。

解题思路:这道题可以理解为每一个字母后面可以出现它本身或者比它大的字母,当然字母出现要符合26个英文字母的出现顺序。我可以使用两个循环,遍历整个字符串,每当后面出现和前面相同的字母,我就标记一下,当再换另一个字母的时候也要判断是否按照26字母排序,以此类推。

上代码:

 1 #include<stdio.h>
 2 #include<string.h>
 3 int main()
 4 {
 5     char s[501],c;
 6     int  v[501];
 7     int i,j,k,flag;
 8     gets(s);
 9     memset(v,0,sizeof(v));
10     flag=1;
11     k=strlen(s);
12     c='a';
13     for(i=0; i<k; i++)
14     {
15         if(v[i]==0)
16         {
17             if(c!=s[i])
18             {
19                 flag=0;
20                 break;
21             }
22             else
23             {
24                 for(j=i; j<k; j++)
25                 {
26                     if(s[j]==s[i])
27                     {
28                         v[j]=1;
29                     }
30                 }
31             }
32             c++;
33         }
34     }
35     if(flag==0)
36         printf("NO
");
37     else
38         printf("YES
");
39     return 0;
40 }

双重循环之中做标记这种方法很是常用,通过判断是否之前出现过或者是否再次被利用,减少了循环次数,缩短了运行时间。

 
原文地址:https://www.cnblogs.com/wkfvawl/p/8716004.html