Log inSign up
Problems / B046

Pottery inscriptions

300 ptsCases 14Set by dongbinna
Added October 30.
1 solved1 attempted1 submissionssolvers took 1.0 tries on average

Statement

A dig site turned up several pieces of pottery, and each piece carries one inscription written in lowercase letters. They lay buried for a long time, so on some pieces a character or two has crept in.

The researcher sorts the inscriptions into three kinds. If reading an inscription from the front and reading it from the back give the same thing, it is an intact inscription, and it is recorded as 0. If reading it from the front and from the back give different things but deleting one character makes it read the same in both directions, it is an inscription broken by one character, and it is recorded as 1. If no single deletion makes it read the same in both directions, it is an unreadable inscription, and it is recorded as 2. The deleted character may sit at any position in the inscription, but only one character may be deleted.

Write a program that, given T inscriptions, decides in order for each one whether it is 0, 1 or 2.

For example, suppose three inscriptions are given. Writing each inscription read from the back along with the decision gives the following table.

InscriptionRead from the backDecisionAnswer
abcbaabcbathe same as it stands0
abcaacbadeleting b gives aca, which reads the same in both directions1
abcabbacbano deletion makes it read the same in both directions2

Look at the second inscription abca a little more closely. The first and the last character are both a, so they agree, but the second character b and the second character from the end c differ. Deleting b here gives aca, which reads the same in both directions. Deleting c gives aba, which also reads the same in both directions. Only one of the two has to work for it to be an inscription broken by one character, so the answer is 1.

The third inscription abcab has a as its first character and b as its last, so it is off from the start. Deleting the first character a gives bcab, which read from the back is bacb, so the two differ. Deleting the last character b gives abca, which read from the back is acba, so the two differ. Deleting a character in the middle leaves the first and the last character as mismatched as before, so the answer is 2.

An inscription of one character reads the same from the front and from the back, so its answer is 0.

Input and Output Examples

Example 1

Input

2
ccacbzbcabaaaabacbbcacc
abcccaacbbcaacccba

Output

1
0

Example 2

Input

1
aaacaabbaaabbaacaaa

Output

0

Example 3

Input

1
qccbyaabaccabaazbccw

Output

2

Input

  • The number of inscriptions T is given first, on the first line. T is a natural number from 1 to 30.

  • From the second line through line T+1, the inscriptions are given one per line. Each inscription is made of lowercase letters only, and the length of one inscription is from 1 to 100,000. No space, uppercase letter or digit appears inside an inscription. One inscription comes per line, so the input is T+1 lines in all.

Output

  • Print one line per inscription, giving the decision in the order the inscriptions are given in the input. Print 0 for an intact inscription, 1 for an inscription broken by one character, and 2 for an inscription that is neither.

  • Print one number per line and then a newline. Do not attach any other character or space before or after it, and do not add a blank line after the last line either.

Requirements

  • Read the input from standard input and write the answer to standard output
  • Only one character may be deleted. An inscription that needs two or more deletions before it reads the same in both directions is 2
  • An inscription that already reads the same in both directions is 0. Writing 1 when no character has to be deleted is a wrong answer
  • Print one line per inscription, in the order given in the input
  • The length of one inscription goes up to 100,000, so trying every position to delete one at a time does not finish in time
  • Grading compares the output with the expected value down to the last character
  • You must write a program that reads from standard input and writes to standard output
  • Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
  • The languages you can use are Python and JavaScript
  • The memory limit is 256MB. The browser does not measure it, it only states it
  • The time limit is set by measuring the setter's reference solution on the same runtime
  • On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
  • There are 14 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
  • You have to pass all 14 cases to solve this.
Read the requirements carefully before solving. Tampering with the grading to make a submission look like a pass can get you sanctioned if caught. The code you submit is published on the leaderboard along with the result.

Examples

3 public cases. Judging also uses hidden cases. You have to pass all 14 cases to solve this.

Input
Expected output
1
aaacaabbaaabbaacaaa
0
1
qccbyaabaccabaazbccw
2
2
ccacbzbcabaaaabacbbcacc
abcccaacbbcaacccba
1
0

Solution

Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.

0 chars1 lineMemory 256MB (recorded only)Reference 169ms

Submitting requires you to log in. Running against the public cases works without one.

You can test with 3 public cases. Submitting runs the hidden cases too.

People who solved this problem

The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.

1bampal2446ms429 charsPythonWindows NT 10.0 / Chrome/156.0.0.0 / 8코어 / 16GB

Log in and you can see the code once you solve this problem.