We need to parse problem: Given a string of '+' and '-', we want to transform it into a "Chain" sequence, defined as alternating symbols: each two neighboring symbols are either '-+' or '+-'. So essentially the string must be alternating between - and +. However note that the definition "each two neighboring symbols of sequence are either '-+' or '+-'" means the sequence must be alternating, but it doesn't specify which symbol it starts with. There are two possible alternating patterns: starting with '+': +-+-+... or starting with '-': -+-+... Both are valid Chains. So we need to change minimal number of symbols to make the string alternating.
Thus for each test case, given string S of length n, we compare it with two target patterns: pattern A: starting with '+', i.e., positions 0-indexed: if i is even => '+', odd => '-'; pattern B: starting with '-', i.e., even => '-', odd => '+'. Then count mismatches between S and each pattern. Answer = min(countA, countB).
Constraints: T up to 7, |S| up to 1e5, so overall up to 7e5 characters. O(n) per test case fine.
Example: S="---+-+-+++" length? Let's compute: positions: 0:'-',1:'-',2:'-',3:'+',4:'-',5:'+',6:'-',7:'+',8:'+',9:'+'. Actually length? Let's count: '---+-+-+++' => characters: 1:'-',2:'-',3:'-',4:'+',5:'-',6:'+',7:'-',8:'+',9:'+',10:'+'? Wait careful: The string is "---+-+-+++". That is: three '-', then '+', then '-', then '+', then three '+'. So length = 3+1+1+1+3 = 9? Actually "---" =3, "+" =1, "-" =1, "+" =1, "+++" =3 total 9? 3+1+1+1+3=9. But the example says change symbol 2 and symbol 9; that suggests length at least 9. 1-indexed: they changed symbol 2 (position 1 in 0-index) and symbol 9 (position 8). So length 9? Possibly 9. So pattern A mismatches? Compute pattern B: even indices (0,2,4,6,8) = '-', odd indices (1,3,5,7) = '+'. For S: indices:0 '-',1 '-',2 '-',3 '+',4 '-',5 '+',6 '-',7 '+',8 '+'. Compare with pattern B: index0: '-' vs '-' ok; index1: '-' vs '+' mismatch; index2: '-' vs '-' ok; index3: '+' vs '+' ok; index4: '-' vs '-' ok; index5: '+' vs '+' ok; index6: '-' vs '-' ok; index7: '+' vs '+' ok; index8: '+' vs '-' mismatch. So mismatches =2. Pattern A mismatches would be more? Possibly 7. So min =2. Yes.
Second example: "-------" length 7. Pattern B: even '-' odd '+'. For S all '-', mismatches at odd positions (1,3,5) =3. Pattern A would have even '+' odd '-', mismatches: all positions? even indices (0,2,4,6) are '+' but S has '-': 4 mismatches; odd indices (1,3,5) are '-' match: 3 mismatches? Actually even positions: 0,2,4,6 => 4 mismatches, odd: 1,3,5 => 3 matches, total mismatches=4. So min=3. So matches example.
Thus solution straightforward.
Implementation: For each test case, read string s. Initialize count1=0, count2=0. For i in range(len(s)): desired char for pattern1: if i%2==0: '+', else '-'. Compare: if s[i]!=desired => count1++. For pattern2: if i%2==0: '-', else '+'. Compare: count2++. Answer = min(count1, count2). Print answer.
Edge cases: string length 1: any single symbol is trivially alternating? Wait definition: "each two neighboring symbols of sequence are either '-+' or '+-'." For a sequence of length 1, there are no neighboring pairs, so vacuously it's a Chain. So we don't need to change anything. Our method: pattern1: for i=0, desired '+' => if s[0] is '+' then count1=0 else 1; pattern2: desired '-' => count2 accordingly. Min would be 0 if s matches either pattern, else 1? But if length 1, both patterns are valid? Actually both patterns are Chains? The condition is about neighboring pairs; there are none, so any single symbol qualifies. So the minimal replacements to receive a Chain should be 0, because the sequence is already a Chain. But our method would give 1 if the symbol doesn't match either pattern? But we must be careful: Are one-symbol strings considered Chains? The definition: "sequence is called Chain if each two neighboring symbols of sequence are either '-+' or '+-'." For length 1, there are no two neighboring symbols, so condition is vacuously true. So indeed length 1 string is Chain. Therefore minimal replacements = 0. But if we consider patterns, we need to check if we can achieve a chain without changes. That means the given string itself is a chain. For length 1, it is. So answer must be 0. Our method returns min(count1, count2) which could be 1 if s[0]='-'? Actually count1: desired '+' => mismatch count1=1; count2: desired '-' => mismatch count2=0? Wait pattern2: even => '-', so if s[0]='-', then count2=0, min=0. If s[0]='+', then count1=0, min=0. So min is 0 anyway because one of the patterns matches the single character. Actually both patterns can't both be mismatched for length 1 because the two patterns are opposite. So one of them will match. Indeed for i=0: pattern1 desires '+', pattern2 desires '-'. So either s[0] is '+' (pattern1 matches) or '-' (pattern2 matches). So min is 0. So that's fine.
Now think about length 0? Not possible because |S|>=1.
Thus solution stands.
Complexities: O(n) per test case, O(total length) time, O(1) extra.
Now implement solve().
We'll read T, loop, read s (strip). Compute n = len(s). Use simple loop.
Potential pitfalls: Input lines may have spaces? No, S is a string of symbols only. Use sys.stdin.
Now produce final answer.
But the problem may have tricky part: The definition "each two neighboring symbols of sequence are either '-+' or '+-'" could be interpreted that the sequence must have alternating symbols, but maybe also could be empty? Already handled.
Thus answer.
But note: There is possibility that we might misinterpret: maybe they want a chain where each neighboring pair is exactly '-+' or '+-', meaning that no two adjacent are the same. That's exactly alternating. So correct.
Thus final solution.
We should write code accordingly.
I'll include function solve() and if name == "main":.
Now test with given examples.
Example 1: s="---+-+-+++", count1? Let's compute quickly: pattern1: even '+' odd '-'. s: idx0='-', desired '+' => mismatch count1=1; idx1='-', desired '-' => match; idx2='-', desired '+' => mismatch => count1=2; idx3='+', desired '-' => mismatch =>3; idx4='-', desired '+' => mismatch =>4; idx5='+', desired '-' => mismatch =>5; idx6='-', desired '+' => mismatch =>6; idx7='+', desired '-' => mismatch =>7; idx8='+', desired '+'? Wait pattern1 at idx8 (even) desired '+', s[8]='+'? Actually s[8] from "---+-+-+++": indexing: 0'-',1'-',2'-',3'+',4'-',5'+',6'-',7'+',8'+',9? Wait length? Let's count precisely: "---+-+-+++". Write characters: positions: 0: '-', 1: '-', 2: '-', 3: '+', 4: '-', 5: '+', 6: '-', 7: '+', 8: '+', 9: '+'? Actually "+++" is three pluses, so after index7='+', we have index8='+', index9='+'? That would be 10 characters? Let's count from string: "---" (3), then "+" (1) =4, then "-" (1)=5, then "+" (1)=6, then "+++" (3)=9. So indices 0-2 = '-', 3='+', 4='-', 5='+', 6='-', 7='+', 8='+', 9='+'? Wait that's 3+1+1+1+3 = 9 characters, so indices 0..8. So indices: 0,1,2 are '-', 3 is '+', 4 is '-', 5 is '+', 6 is '-', 7 is '+', 8 is '+'. But that would be only 2 pluses at the end? Actually we said "+++" three pluses, so indices 6? Let's break: The string: "---+-+-+++". Write with separators: characters: 1: '-', 2: '-', 3: '-', 4: '+', 5: '-', 6: '+', 7: '-', 8: '+', 9: '+', 10: '+'. That's 10? The string literal: "---+-+-+++" after the hyphen? Let's count: The pattern is: three hyphens, then a plus, then a hyphen, then a plus, then three pluses. So total = 3+1+1+1+3 = 9. So index mapping: indices 0,1,2 = hyphens; index3 = plus; index4 = hyphen; index5 = plus; index6,7,8 = pluses? That gives 3 pluses at the end: indices 6,7,8. But then the hyphens after index5? Actually after index5 which is plus, we have "-"? Wait the substring is "---+-+-+++". Let's write it as: ' - - - + - + + +'? That seems ambiguous. Actually the substring after the first three hyphens: "+-+"? The pattern includes " + - + " and then "+++". So the characters: positions: 0: '-', 1: '-', 2: '-', 3: '+', 4: '-', 5: '+', 6: '+', 7: '+', 8: '+'. But that would only have two pluses after index5? Wait we need three pluses: "+++". So perhaps it's: indices: 6:'+',7:'+',8:'+'. So yes indices 6,7,8 are '+'. So index6 is '+', not '-'. But earlier I wrote index6='-'. Let's parse exactly: "---+-+-+++". Split into parts: "---" (hyphens), then "+" (plus), then "-" (hyphen), then "+" (plus), then "+++" (three pluses). So after "---" (3 chars) we have position 3 = '+', position 4 = '-', position 5 = '+', then positions 6,7,8 = '+', '+', '+'. So string: 0 '-',1 '-',2 '-',3 '+',4 '-',5 '+',6 '+',7 '+',8 '+'. Yes. So the earlier index6 '-' was wrong; it's '+'. Then we can compute pattern1 mismatches: index0: '-' vs '+' mismatch (1); index1: '-' vs '-' match; index2: '-' vs '+' mismatch (2); index3: '+' vs '-' mismatch (3); index4: '-' vs '+' mismatch (4); index5: '+' vs '-' mismatch (5); index6: '+' vs '+' match; index7: '+' vs '-' mismatch (6); index8: '+' vs '+' match. So count1=6? Actually I counted index7 mismatch: pattern1 at odd index (7) desired '-', but s[7]='+' so mismatch =>6. So count1=6. Pattern2 mismatches we computed earlier: index0 '-' vs '-' ok; index1 '-' vs '+' mismatch (1); index2 '-' vs '-' ok; index3 '+' vs '+' ok; index4 '-' vs '-' ok; index5 '+' vs '+' ok; index6 '+' vs '-' mismatch? index6: pattern2 even? index6 even? 6%2=0 => desired '-', s[6]='+' => mismatch (2); index7 odd desired '+'? pattern2 odd: '+', s[7]='+' => ok; index8 even desired '-', s[8]='+' => mismatch (3). Actually earlier I had index8 mismatch? s[8]='+' vs '-', so mismatch -> count2=3? Wait recalc: pattern2: even indices (0,2,4,6,8) => '-', odd indices (1,3,5,7) => '+'. S: [0]'-' ok; [1]'-' should be '+' -> mismatch (1); [2]'-' ok; [3]'+' ok; [4]'-' ok; [5]'+' ok; [6]'+' should be '-' -> mismatch (2); [7]'+' ok; [8]'+' should be '-' -> mismatch (3). So count2=3. But earlier I said count2=2. Let's recount earlier I had index8 mismatch? In earlier quick calculation I thought s[8]='+', pattern2 even '-', mismatch indeed, but I counted only 2 mismatches: index1 and index9 (nonexistent). I forgot index6. So correct count2=3. But example answer is 2. Something off.
Wait the example says: We can change symbol 2 from '-' to '+' and symbol 9 from '+' to '-' and receive '-+-+-+-+-+'. That suggests final length is 10? Because '-+-+-+-+-+' has 10 characters? Let's count: '-+-+-+-+-+': alternating starting with '-', length: pattern: positions:1 '-',2 '+',3 '-',4 '+',5 '-',6 '+',7 '-',8 '+',9 '-',10 '+'? Actually "-+-+-+-+-+" if we count: characters: 1:'-',2:'+',3:'-',4:'+',5:'-',6:'+',7:'-',8:'+',9:'-',10:'+'. That's 10 characters. So the target chain length is 10. And they changed symbol 2 and symbol 9, meaning they changed positions 2 and 9 (1-indexed). So original length must be at least 9? Actually if they changed symbol 9, there must be at least 9 symbols. Could be 9? If length 9, symbol 9 exists. After changing symbol 2 and 9, they got a chain of length 9? But the chain they show is '-+-+-+-+-+' which has 10 characters if we count carefully? Let's count: '-+-+-+-+-+' appears to have alternating characters and ends with '+'. How many characters? Write it: dash, plus, dash, plus, dash, plus, dash, plus, dash, plus. That's 10. But if they had 9 characters, the pattern might be '-+-+-+-+?' Not sure.
Let's re-express: Example case 1 input: "---+-+-+++" maybe it has length 10? Let's count the string manually:
String: ---+-+-+++
Write each character: 1: '-', 2: '-', 3: '-', 4: '+', 5: '-', 6: '+', 7: '-', 8: '+', 9: '+', 10: '+'. That's 10 characters? Because after "---" (3), then "+" (1) =>4, then "-" (1)=>5, then "+" (1)=>6, then "+++" (3) =>9? Wait my count 3+1+1+1+3 = 9, but I listed 10? Let's write the string as a sequence: characters: position1: '-', position2: '-', position3: '-', position4: '+', position5: '-', position6: '+', position7: '-', position8: '+', position9: '+', position10: '+'? That would be 3 hyphens, then plus, hyphen, plus, then three pluses? That would be 3+1+1+1+3 = 9, not 10. So why do I have 10? Because maybe "+++" is three pluses, so after position7 '-', we have three pluses: positions 8,9,10. But then that totals: positions 1-3: hyphens (3), 4: plus, 5: hyphen, 6: plus, 7: hyphen, 8: plus, 9: plus, 10: plus => that's 3+1+1+1+1+? Let's break down string "---+-+-+++":
The string is composed: "---" (three hyphens)
Then "+" (one plus)
Then "-" (one hyphen)
Then "+" (one plus)
Then "+++" (three pluses)
So total characters = 3 + 1 + 1 + 1 + 3 = 9.
But after the hyphen at position 5? Actually the ordering:
Index: 0: '-'
1: '-'
2: '-'
3: '+'
4: '-'
5: '+'
6: '+'
7: '+'
8: '+'? Wait if there are three pluses at the end, then after index5 '+', we need three pluses. So indices 6,7,8 should be pluses. That gives 9 indices: 0..8. So indices: 0 '-',1 '-',2 '-',3 '+',4 '-',5 '+',6 '+',7 '+',8 '+'. That's 5 hyphens? Actually there are hyphens at indices 0,1,2,4: that's four hyphens? Index0,1,2 are hyphens (three), index4 is hyphen (one) -> total 4 hyphens? Wait we have at index4 hyphen, yes. So hyphens count: indices 0,1,2,4 => 4 hyphens. Plus count: indices 3,5,6,7,8 => 5 pluses. So total 9 characters. But then the example says change symbol 2 (position2 1-indexed) from '-' to '+', and symbol 9 (position9) from '+' to '-'. But if length is 9, position9 is the last character, which is '+' (index8). Changing that to '-' would yield final string: after changes: position2 becomes '+', position9 becomes '-'. So original: positions (1-indexed):
1: '-'
2: '-' -> change to '+'
3: '-'
4: '+'
5: '-'
6: '+'
7: '-'
8: '+'
9: '+' -> change to '-'
After changes:
1: '-', 2: '+', 3: '-', 4: '+', 5: '-', 6: '+', 7: '-', 8: '+', 9: '-'
That yields '-+-+-+-+-' which is alternating and ends with '-'. That's 9 characters: '-+-+-+-+-' (starting with '-', then plus, minus, plus, minus, plus, minus, plus, minus). That's a valid chain. But the example printed '-+-+-+-+-+', which is 10 characters? Actually '-+-+-+-+-+' ends with '+', which would be length 10 if we have alternating starting with '-' and ending with '+', i.e., odd length would end with opposite of start if length even? Let's analyze: For alternating starting with '-': pattern: index0 '-',1 '+',2 '-',3 '+',4 '-',5 '+',6 '-',7 '+',8 '-',9 '+', ... So length 9 (indices 0-8) ends with '-'? Actually if start with '-', then even indices are '-', odd indices are '+'. For length 9 (indices 0..8), index8 is even, so char is '-'. So chain of odd length starting with '-' ends with '-'. So '-+-+-+-+-' (9 chars) ends with '-'. The example shows '-+-+-+-+-+' which ends with '+', that's even length (10). So they might have a 10-length string. Could it be that the input string has length 10? Let's recalc the input: "---+-+-+++" - maybe it's actually 10 characters? Could be that the string is "---+-+-+++" meaning three hyphens, then a plus, then a hyphen, then a plus, then two pluses? Actually "+++" is three pluses, but maybe it's "++" with an extra? Let's type the string: '-', '-', '-', '+', '-', '+', '-', '+', '+', '+'? That's what I had before: 10 characters if we include an extra hyphen? But the substring after +-? The pattern "+-+" appears? Actually "---+-+-+++" contains "---", then "+", then "-", then "+", then "+++". That is 3+1+1+1+3=9. So it's 9. So the example might have a typo? Or maybe they changed symbol 9 (the ninth) from '+' to '-' and got '-+-+-+-+-' but they wrote '-+-+-+-+-+' for brevity? Or maybe the string is longer? Let's check sample input:
2
---+-+-+++
The output is 2 and 3. So likely the correct minimal changes for first case is 2. Our computed count2 for pattern B (starting with '-') we got 3 mismatches, but we earlier got 2? Let's recalc pattern B mismatches carefully for S of length 9 as we defined (0-index: 0:'-',1:'-',2:'-',3:'+',4:'-',5:'+',6:'+',7:'+',8:'+'). But wait, is index6 a '+'? According to our decomposition, yes, because after index5 '+', the next three are pluses. But the string "---+-+-+++" includes a hyphen after the plus? Actually the substring "+-+" suggests that after the hyphen there is a plus, but then there are three pluses? Let's break down string exactly: write characters of the string:
The string: ---+-+-+++
Let's count by scanning:
Character 1: '-'
2: '-'
3: '-'
4: '+'
5: '-'
6: '+'
7: '-'
8: '+'
9: '+'
10: '+'?
Wait there is an extra hyphen? Because the pattern after "---" we have "+-+" which is plus, hyphen, plus. That's three characters: plus, hyphen, plus. So after the three hyphens, we have plus, hyphen, plus. Then "+++" three pluses. So that would be: hyphens: positions 1,2,3; then position4: '+'; position5: '-'; position6: '+'; then positions 7,8,9: '+', '+', '+'. That totals 9? Actually positions 1-3 = 3, positions 4-6 = 3, positions 7-9 = 3, total 9? No, 3+3+3 = 9? That's 9: 3 hyphens, then 3 characters (+, -, +), then 3 pluses. So positions: 1 '-',2 '-',3 '-',4 '+',5 '-',6 '+',7 '+',8 '+',9 '+'. So index5? Actually index6 becomes '+'? Let's index: 0:'-',1:'-',2:'-',3:'+',4:'-',5:'+',6:'+',7:'+',8:'+'. So yes.
But wait, we have a hyphen at position5? Actually position5 (1-index) is '-', yes. So the pattern contains a hyphen at position5. Then position6 is '+'. So we have "-+" at positions 5-6. So the string has "...-+..." Then positions 7,8,9 are pluses. So indeed index6 is '+'? Actually index6 (0-index) corresponds to position7? Let's map: 0-index i => position i+1. So:
i=0 -> pos1 '-'
i=1 -> pos2 '-'
i=2 -> pos3 '-'
i=3 -> pos4 '+'
i=4 -> pos5 '-'
i=5 -> pos6 '+'
i=6 -> pos7 '+'
i=7 -> pos8 '+'
i=8 -> pos9 '+'
So yes.
Now pattern B: start with '-': pattern: even i -> '-', odd i -> '+'.
Compare:
i=0: s='-', expected '-' => OK.
i=1: s='-', expected '+' => mismatch (count=1)
i=2: s='-', expected '-' => OK
i=3: s='+', expected '+' => OK
i=4: s='-', expected '-' => OK
i=5: s='+', expected '+' => OK
i=6: s='+', expected '-' => mismatch (count=2)
i=7: s='+', expected '+' => OK? i=7 odd, expected '+' => s='+' OK.
i=8: s='+', expected '-' => mismatch (count=3)
So count2 = 3 mismatches. But example says answer=2. So maybe I mis-identified the characters. Perhaps the string is actually "---+-+-+++" with length 10? Let's recount by actually typing: The sample input line: "---+-+-+++". Possibly there is an extra character like a newline? But we must trust the problem.
Maybe the string is: "---+-+-+++" where after "---" we have "+-+" meaning plus, hyphen, plus, but then "+++" meaning plus, plus, plus? But that would be plus, hyphen, plus, plus, plus, plus? Actually if we parse as "---" then "+-+" then "+++" we get overlapping? The string literal: "---+-+-+++". Write it as characters:
1: '-'
2: '-'
3: '-'
4: '+'
5: '-'
6: '+'
7: '-'
8: '+'
9: '+'
10: '+'
But there is an extra hyphen? Because between position4 '+' and position6 '+' there should be a hyphen at position5. So we have position5 '-', yes. So that yields: positions: 1 '-', 2 '-', 3 '-', 4 '+', 5 '-', 6 '+', 7 '-', 8 '+', 9 '+', 10 '+'. That is 3 hyphens, then plus, hyphen, plus, hyphen, plus, plus, plus. That's 10 characters: hyphens at 1,2,3,5,7? Actually positions: 1 '-',2 '-',3 '-',5 '-',7 '-': that's five hyphens? Let's list: 1 '-'; 2 '-'; 3 '-'; 4 '+'; 5 '-'; 6 '+'; 7 '-'; 8 '+'; 9 '+'; 10 '+'. Hyphens at 1,2,3,5,7 = 5 hyphens; pluses at 4,6,8,9,10 = 5 pluses. So that's length 10. Could that be? Let's verify the string: "---+-+-+++". How many characters if we just count: each character: type them:
' - '
' - '
' - '
' + '
' - '
' + '
' - '
' + '
' + '
' + '
Yes, that seems to be 10. But earlier I thought "+-+" is three characters, but the string has "-+-" after the first three hyphens? Actually the substring after "---" is "+-+"?? Wait the string is "---+-+-+++". After the first three hyphens, the next characters are "+", then "-", then "+", then "-", then "+", then "+", then "+"? Let's break using the literal: "---+-+-+++". Write it with indices:
Index:0: '-'
1: '-'
2: '-'
3: '+'
4: '-'
5: '+'
6: '-'
7: '+'
8: '+'
9: '+'
I count 10? Actually index9 means 10 characters if we start at 0. So indices 0..9 inclusive = 10 characters. I gave indices 0-9? I listed up to index9 as '+', but I only listed indices up to 8 earlier? Let's count properly: The string "---+-+-+++" length? Let's calculate using Python mentally: len("---+-+-+++") = count of characters: three hyphens (3), then plus (1) =>4, then hyphen (1)=>5, then plus (1)=>6, then hyphen (1)=>7, then plus (1)=>8, then plus (1)=>9, then plus (1)=>10. Yes, because after the "---", we have "+-+" which is plus, hyphen, plus (three chars), then "+++" three pluses. But that sums to 3 + 3 + 3 = 9? Wait 3 hyphens, plus-hyphen-plus is 3, plus three pluses is 3, total 9. But we just counted plus-hyphen-plus as three, then three pluses, but I counted an extra hyphen? Let's examine substring: "---+-+-+++". Split: "---" = 3. Then the rest is "+-+-+++"? Actually the string is "---+-+-+++" meaning after "---" we have "+-+-+++"? The pattern could be "---" + "+-+" + "-+++"? I'm getting confused.
Better to actually write the string explicitly: The characters are:
1: -
2: -
3: -
4: +
5: -
6: +
7: -
8: +
9: +
10: +
But why is there a hyphen at 7? Because after position6 '+', position7 is '-'? According to my listing, at index5 (0-based) we have '+' (position6), index6 (pos7) is '-', index7 (pos8) is '+', index8 (pos9) '+', index9 (pos10) '+'. So we have a hyphen at index6 (pos7). That hyphen is part of the substring "-+" maybe? The substring from index4 to index6 is "- + -"? Actually index4 is '-', index5 is '+', index6 is '-', so that's "-+-". That seems plausible: after the first three hyphens, we have "+-+-" maybe? Let's examine the string: "---+-+-+++". If I read it as: start: --- then + then - then + then - then ++? Actually there is ambiguity. Let's just treat it as a string and count length: In many programming problems, the sample "---+-+-+++" is often length 10. I can quickly check by counting characters in the provided example: In the explanation, they say change symbol 2 and symbol 9. If length is 10, symbol 9 is the ninth character, and symbol 2 is second. After changes, they claim '-+-+-+-+-+', which is length 10. So original length must be 10. So indeed the string is of length 10. So our earlier decomposition of "---+-+-+++" as 3 hyphens, then plus, then hyphen, then plus, then hyphen, then plus, then plus, then plus? That would be 3 + 1 + 1 + 1 + 1 + 1 + 1 + 1? No.
Let's manually count the string as typed:
- at position1
- at pos2
- at pos3
That seems to be: "---+-+-+++" = "--- + - + - + ++"? Actually after three hyphens, we have a plus, then a hyphen, then a plus, then a hyphen, then two pluses? Wait we have three pluses at the end: pos8, pos9, pos10 are plus, plus, plus. So pos8 is plus, pos9 plus, pos10 plus. So after the hyphen at pos7, we have three pluses. But the pattern shows a hyphen at pos7, then plus at pos8, then plus at pos9, then plus at pos10. So that's "-+++". So the substring before that is "---+-+-", i.e., three hyphens, plus, hyphen, plus, hyphen. So "---+-+-" then "+++". That yields total: 3 hyphens (---), then +, -, +, -, then +++: So that's 3 + 4 + 3 = 10? Actually "---" =3, "+-+-" =4? Let's break: after "---", we have characters: '+', '-', '+', '-'. That's 4 characters, total 7, then "+++" adds 3, total 10. So the string is "---+-+-+++"? But that would be "---+-+-+++"? Actually "---+-+-+++" includes an extra hyphen? Let's write "---+-+-+++": characters: 1 '-',2 '-',3 '-',4 '+',5 '-',6 '+',7 '-',8 '+',9 '+',10 '+'. Yes, that's exactly that. So the string is "---+-+-+++". So it's 3 hyphens, then "+-+", then "-+++"? Actually the substring "+-+" appears starting at position4? Position4 '+', position5 '-', position6 '+' gives "+-+", that's three characters. Then position7 '-', then "+++" (positions 8-10) gives "-+++". So the string can be seen as "---" + "+-+" + "-+++". But then the hyphen at position7 is extra. So it's not simply three blocks of three; it's 3 + 3 + 4? No.
Anyway, it's clear the length is 10. So my earlier confusion stemmed from mis-grouping.
Thus for length 10, our earlier pattern B calculation should yield 2 mismatches, not 3. Let's recalc with length 10.
Define s as: indices 0..9:
0: '-'
1: '-'
2: '-'
3: '+'
4: '-'
5: '+'
6: '-'
7: '+'
8: '+'
9: '+'
Now pattern B (start with '-'): even indices: 0,2,4,6,8 => '-'; odd indices: 1,3,5,7,9 => '+'.
Compare:
i0: '-' vs '-' OK
i1: '-' vs '+' mismatch (1)
i2: '-' vs '-' OK
i3: '+' vs '+' OK
i4: '-' vs '-' OK
i5: '+' vs '+' OK
i6: '-' vs '-' OK? Wait i6 even, expected '-', s[6] = '-'? Yes, s[6] = '-'? In our list s[6] = '-'? Let's check: s[6] corresponds to position7, which we said is '-'. So s[6] = '-' matches expected '-'. Good.
i7: '+' vs '+' OK
i8: '+' vs '-' mismatch (2)
i9: '+' vs '+'? i9 odd expected '+', s[9]='+' OK.
Thus mismatches = 2. So count2=2, count1 would be higher. So min=2. So matches example.
Thus our algorithm is correct.
Now the second example: "-------" length 7, we computed min=3, matches.
So solution stands.
Now coding: we just need to read T, then loop, compute.
But note constraints: up to 1e5 per string, and T up to 7, so total up to 7e5, easy.
Implementation in Python:
def min_changes(s):
n = len(s)
cnt1 = 0 # pattern + - + - ...
cnt2 = 0 # pattern - + - + ...
for i,ch in enumerate(s):
if i%2 == 0:
if ch != '+': cnt1 += 1
if ch != '-': cnt2 += 1
else:
if ch != '-': cnt1 += 1
if ch != '+': cnt2 += 1
return min(cnt1, cnt2)
Alternatively we can compute using sum and list comprehensions but loop is fine.
Edge Cases: None.
Now produce final answer.
But wait: The problem statement says "minimum number of symbols he need to replace" meaning we can change symbols arbitrarily. Yes.
Now produce final code.
I'll include reading from sys.stdin, writing to sys.stdout.
Now final answer.
But let's reflect: Could there be any twist? For instance, maybe they consider that a Chain is defined as each two neighboring symbols are either '-+' or '+-' which strictly requires that the sequence must have alternating symbols, but also maybe they require that the entire sequence is exactly that pattern, not that it can start with either? But they gave example: they changed to '-+-+-+-+-+', which starts with '-'. So both are allowed.
Thus fine.
Now code.
But since this is a typical problem, we can also compute without enumerating both patterns: we could compute number of mismatches if we assume pattern start with '+', and the other is n - mismatches? Not exactly, because flipping all bits? Actually the two patterns are complementary. For a given string, the sum of mismatches for pattern1 and pattern2 is equal to n, because for each position, exactly one of the patterns matches the character? Let's check: For a given position i, pattern1 expects either '+' (if i even) or '-' (if i odd). Pattern2 expects the opposite: '-' if i even, '+' if i odd. So for any character ch, exactly one of these expectations matches ch? Not necessarily: Suppose ch is '+', and at even i, pattern1 expects '+', pattern2 expects '-'; so only pattern1 matches. At odd i, pattern1 expects '-', pattern2 expects '+'; only pattern2 matches. So yes, for any given ch, exactly one of the two patterns matches. Because the two patterns are complements. Therefore for each index, mismatches: pattern1 mismatch is 1 if ch != expected1, else 0; pattern2 mismatch is 1 if ch != expected2, else 0. Since expected1 != expected2, and ch is either expected1 or expected2? Actually ch could be something else? No, ch is either '+' or '-'. So ch is exactly one of expected1 or expected2. So exactly one pattern will have mismatch 0 and the other mismatch 1. So for each index, pattern1 mismatch + pattern2 mismatch = 1. Therefore sum of mismatches over all indices = n. So count2 = n - count1. So we could compute only one pattern and then answer = min(count1, n-count1). This reduces code but not necessary. But it's a good observation.
Nevertheless, implement either way.
But careful: Is it always true that expected1 != expected2? Yes, because they are opposite: for each i, expected1[i] = '+' if i even else '-', expected2[i] = '-' if i even else '+', so they are opposite. And ch is either '+' or '-', so indeed exactly one matches. So yes.
Thus we can compute count1 (mismatches with pattern starting with '+') and answer = min(count1, n - count1).
But for clarity, I'll implement both; but it's okay.
Now produce final answer.
I'll also handle potential large input using sys.stdin.read or sys.stdin.buffer.
Implementation:
import sys
def solve():
data = sys.stdin.read().split()
if not data: return
t = int(data[0])
out = []
idx = 1
for _ in range(t):
s = data[idx]
idx += 1
n = len(s)
cnt = 0
for i, ch in enumerate(s):
if i % 2 == 0:
if ch != '+':
cnt += 1
else:
if ch != '-':
cnt += 1
# cnt is mismatches for pattern starting with '+'
# mismatches for pattern starting with '-' = n - cnt
out.append(str(min(cnt, n - cnt)))
sys.stdout.write("\n".join(out))
if name == "main":
solve()
Test with sample:
Input:
2
---+-+-+++
Our code: for first string, compute cnt. Let's compute: s = "---+-+-+++" length 10.
i=0 even, ch='-', expected '+' -> cnt=1
i=1 odd, ch='-', expected '-'? Actually for pattern starting with '+', odd positions should be '-', so ch='-' matches? At i=1 odd: expected '-', ch='-' -> no increment? Actually condition: if i%2 != 0: if ch != '-' then cnt++. Since ch='-', not increment. So cnt stays 1.
i=2 even, ch='-', expected '+' -> cnt=2
i=3 odd, ch='+', expected '-' -> ch != '-' => cnt=3
i=4 even, ch='-', expected '+' -> cnt=4
i=5 odd, ch='+', expected '-' -> cnt=5
i=6 even, ch='-', expected '+' -> cnt=6
i=7 odd, ch='+', expected '-' -> cnt=7
i=8 even, ch='+', expected '+'? Wait i=8 even, expected '+', ch='+' matches, no increment. cnt stays 7.
i=9 odd, ch='+', expected '-' -> cnt=8. So cnt=8. n-cnt = 2. min=2. Correct.
Second string: "-------" length 7.
i0 even, ch='-', expected '+' -> cnt=1
i1 odd, ch='-', expected '-' -> match, cnt=1
i2 even, ch='-', expected '+' -> cnt=2
i3 odd, ch='-', expected '-' -> match, cnt=2
i4 even, ch='-', expected '+' -> cnt=3
i5 odd, ch='-', expected '-' -> match, cnt=3
i6 even, ch='-', expected '+' -> cnt=4
cnt=4, n-cnt=3, min=3. Correct.
So it works.
Now final answer: Provide explanation and code.
But the problem statement is simple, no need for advanced.
Thus final. ```python
import sys
def solve() -> None:
data = sys.stdin.read().split()
if not data:
return
t = int(data[0])
out_lines = []
idx = 1
for _ in range(t):
s = data[idx]
idx += 1
n = len(s)
cnt = 0 # mismatches with pattern '+ - + - ...'
for i, ch in enumerate(s):
if i % 2 == 0:
if ch != '+':
cnt += 1
else:
if ch != '-':
cnt += 1
# mismatches with the other pattern is n - cnt
out_lines.append(str(min(cnt, n - cnt)))
sys.stdout.write("\n".join(out_lines))
if name == "main":
solve()